【发布时间】:2016-12-07 17:24:26
【问题描述】:
我正在使用 API 将 Json 数据转换为对象数据, 请访问我正在使用的 API Please Visit the API page which I am using
这是我的代码的快照,其中突出显示了问题 Issues
这是原始代码
-(void) retrieveData{
NSURL * url = [NSURLURLWithString:getDataUrl];
NSData * data = [NSData dataWithContentsOfURL:url]; jsonArray = [NSJSONSerialization JSONObjectWithData:data options: kNilOptions error:nil]; NSLog(@"JsonArray %@", jsonArray); //setup yougaArray yougaArray = [[NSMutableArray alloc] init]; //Loop through our jsonArray for (int i = 0; i<jsonArray.count; i++) { NSString * yId = [[[[jsonArray objectAtIndex:i]objectForKey:@"data"]objectAtIndex:@"categories"]objectForKey:@"id"]; // NSString * yId = [[jsonArray objectAtIndex:i]objectForKey:@"id"]; NSString * yName = [[[[jsonArray objectAtIndex:i]objectForKey:@"data"]objectAtIndex:@"categories"]objectForKey:@"name"]; NSString * yDescription = [[[[jsonArray objectAtIndex:i]objectForKey:@"data"]objectAtIndex:@"categories"]objectForKey:@"description"]; NSString * yImage = [[[[jsonArray objectAtIndex:i]objectForKey:@"data"]objectAtIndex:@"categories"]objectForKey:@"image"]; //Add the city object to our citiesArray [yougaArray addObject:[[Youga alloc]initWithYougaId:yId andYougaName:yName andYougaDescpription:yDescription andYougaImage:yImage]]; } [self.tableView reloadData]; }
【问题讨论】:
-
objectAtIndex:@"categories"=> 为什么要放一个 NSString 呢?此外,在尝试解析 JSON 之前,您需要了解它的结构。 -
你能打印
NSLog(@"JsonArray %@", jsonArray);] -
你能像这样访问数组吗
-
你能找到一个不到五年的教程吗?没有理由不使用
[]索引。 -
那我现在该怎么办?我浏览了 API,在那里我使用了 objectAtIndex:@"something" where is" [ " used。并使用 ObjectForKey:@"something" where "{ " used.
标签: ios objective-c json nsmutablearray nsmutablestring