2011-12-17 55 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"]; 

但是,请记住,我只是乞丐,所以这可能不是最有效的方式:)好运!