2011-12-17 52 views
1

顯示它我有這個網址:http://maps.google.com.br/maps/api/directions/json?origin=porto+alegre&destination=novo+hamburgo&sensor=false故障的解析JSON和UITableView的

而這種代碼:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
[connection release]; 

NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
[responseData release]; 

NSMutableDictionary *theDictionary = [responseString JSONValue]; 

NSLog(@"%@", theDictionary); 

[responseString release]; 
if (theDictionary != nil) 
{ 
    NSArray *routesArray = [theDictionary objectForKey:@"routes"]; 
    NSDictionary *firstRoute = [routesArray objectAtIndex:0]; 
    NSArray *legsArray = [firstRoute objectForKey:@"legs"]; 
    NSDictionary *firstLeg = [legsArray objectAtIndex:0]; 
    steps = [firstLeg objectForKey:@"steps"]; 
} 

[tableView reloadData]; 

}

我想顯示每一個@在 「html_instructions」 UITableView的。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
// Return the number of rows in the section. 
return [self.steps count]; 

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cachedCell"]; 

if (cell == nil) 
    cell = [[[UITableViewCell alloc] 
      initWithFrame:CGRectZero reuseIdentifier:@"cachedCell"] autorelease]; 

return cell; 

}

缺少了什麼在我的代碼???應該是一些小細節...任何幫助將是偉大的!

+0

'steps'是一個NSMutableArray?獲取JSON數據嗎?請清楚解釋。 – Emon 2011-12-20 04:04:37

+0

是的。 'steps'是nsmutablearray,json解析得很好(nslog)。 @Emon – libchief 2011-12-20 19:10:54

回答

0

不要你必須做這樣的事情這在你的CellForRowAtIndexPath?

NSString *cellValue = **WHATEVER VALUE YOU WANT TO PUT IN**; 

cell.textLabel.text = cellValue; 

return cell; 

我沒有看到你將數據添加到單元格。

1

我會做這樣的: 有你網站URL創建URL請求

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init]; 
     [request setURL:[NSURL URLWithString:@"http://maps.google.com.br/maps/api/directions/json?origin=porto+alegre&destination=novo+hamburgo&sensor=false"]]; 

然後創建NSData的結構,保存響應請求,我又把它轉換成字符串

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; //Or async request 
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 

後您將JSON加載到Dictionary中,然後向下移動一級,將其傳遞到另一個字典(結果1)

NSError *error=nil; 
    result = [NSJSONSerialization JSONObjectWithData:returnData options:kNilOptions error:&error]; 
    result1 = [result objectForKey:@"routes"]; 
    result2 = [result1 objectForKey:@"legs"]; 

然後將其移動到NSMutableArray的

jsonMutArray= [result2 objectForKey:@"steps"]; 

從那裏,你只要把標籤到您的自定義單元格附上它作爲一個IBOutlet中,synthetize和的cellForRowAtIndexPath方法做

cell.yourLabel.text = [jsonMutArray objectForKey:@"html_instructions"]; 

但是,請記住,我只是乞丐,所以這可能不是最有效的方式:)好運!