[英]parsing a nested Json in IOS
i just get started with Ios programming, and i have to parse a nested Json file and extract the items and lables, the problem is i have to display the list of the items on a tableview and refresh my tableview everytime i click on a row of my table view: i did display the first list of my items but i'm trying to display my nested items and display them on my "new" tableview: here are a simple of my JSON code and my objective-c code: 我刚开始使用Ios编程,我必须解析一个嵌套的Json文件并提取项目和标签,问题是我必须在每次单击一行时在tableview上显示项目列表并刷新我的tableview。我的表格视图:我确实显示了我的物品的第一个列表,但是我试图显示嵌套的物品并将它们显示在我的“新”表格视图中:这是我的JSON代码和Objective-C代码的简单示例:
"results": {
"items": [
{
"id": "0100",
"label": "Actualités",
"cover": "http://XXXX_01.jpg",
"coverFrom": "XXX-03",
"coverTo": "2031-CCC18",
"coverOrder": 1,
"items": [
{
"id": "0101",
"label": "Actualité / Infos",
"cover": "http://XXXX.jpg",
"coverFrom": "2014XXX-24",
"coverTo": "2031-XXXX18",
"coverOrder": 1,
"items": [
]
},
and my RootViewContoller contain this code of the function connectionDidFinishiongLoading** 和我的RootViewContoller包含该函数connectionDidFinishiongLoading **的代码
// we will follow the format of our nested JSON
NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:dataRequest options:0 error:nil];
NSDictionary *results = [allDataDictionary objectForKey:@"results"];
NSArray *arrayOfItems = [results objectForKey:@"items"];
for (NSDictionary *diction in arrayOfItems) {
//NSArray *ide = [diction objectForKey:@"id"];
NSString *label = [diction objectForKey:@"label"];
// add new object founded
[array addObject:label];
}
// reload my tableview
[[self tableView]reloadData];
// myParsingResults = [NSJSONSerialization JSONObjectWithData:dataRequest options:nil error:nil];
[[self tableView]reloadData];
If I understood correctly, try something like that: 如果我理解正确,请尝试以下操作:
NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:dataRequest options:0 error:nil];
NSDictionary *results = [allDataDictionary objectForKey:@"results"];
NSArray *array = [results objectForKey:@"items"];
NSMutableArray *arrayLabels = [[NSMutableArray alloc] init];
NSDictionnary *dicoTemp = [array objectAtIndex:0];
while([dicoTemp objectForKey:@"items"])
{
arrayLabels addObject:[[dicoTemp objectForKey:@"items"] objectForKey:@"label"];
NSArray *arrayTemp = [dicoTemp objectForKey:@"items"];
dicoTemp = [arrayTemp objectAtIndex:0]; //I don't know how you JSON ends, you may have to check if [arrayTemp count] > 0. I presumed that at the end, there was no object for key "items"
}
[[self tableView] reloadData];
That a weird nested JSON response, in my opinion. 在我看来,这是一个奇怪的嵌套JSON响应。
Try this one... 试试这个...
NSError *error = nil;
NSURL *Url = [[NSURL alloc] initWithString:@"Your URL"];
jsonData = [NSData dataWithContentsOfURL:Url options:NSDataReadingUncached error:&error];
if(jsonData==Nil){
UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"Error" message:@"Please check your internet connection" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
[alert show];
}
else{
NSDictionary *dataDictionary =[[NSDictionary alloc]init];
dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
[array addObject:[[[dataDictionary objectForKey:@"results"] valueForKey:@"items"] valueForKey:@"label"]];
}
and reloads your tableview. 并重新加载您的tableview。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.