这里我有一些json code.with 2 dict of data.like
name1
name2
lap1
lap2
只有label1,label2显示值。 Label3,label4没有显示值!
答案 0 :(得分:2)
使用此
NSArray *jsonObject;
jsonObject = @[@{@"Id1":@"mad",
@"people2":@"12",@"total2":@"20"},
@{@"Id2":@"normal",
@"people3":@"13",@"total3":@"30"}];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonObject options:NSJSONWritingPrettyPrinted error:nil];
NSError *error = nil;
NSArray *arr = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
NSLog(@" JSON DATA \n %@",arr);
for (int i = 0; i < arr.count; i++) {
NSArray *keys = [[arr objectAtIndex:i] allKeys];
NSLog(@"%@",[[arr objectAtIndex:i] valueForKey:[keys objectAtIndex:0]]);
NSLog(@"%@",[[arr objectAtIndex:i] valueForKey:[keys objectAtIndex:1]]);
NSLog(@"%@",[[arr objectAtIndex:i] valueForKey:[keys objectAtIndex:2]]);
//assign your label here line yourLabel.text = [[arr objectAtIndex:i] valueForKey:[keys objectAtIndex:0]];
}
注意:它适用于有3个键的字典
答案 1 :(得分:2)
NSData&#39; jsonData&#39;不可遍历,但您可以遍历jsonObject,即包含字典对象的NSArray。
label1.text = [jsonObject[0] objectForKey:@"people1"];
label2.text = [jsonObject[0] objectForKey:@"total1"];
label3.text = [jsonObject[1] objectForKey:@"people2"];
label4.text = [jsonObject[1] objectForKey:@"total2"];
答案 2 :(得分:1)
使用此代码:
label1.text = [[jsonObject objectAtIndex:0] valueForKey:@"people2"];
label2.text = [[jsonObject objectAtIndex:0] valueForKey:@"total2"];
label3.text = [[jsonObject objectAtIndex:1] valueForKey:@"people3"];
label4.text = [[jsonObject objectAtIndex:1] valueForKey:@"total3"];
答案 3 :(得分:1)
你的错!你在jsonArray中有2个词典
NSDictionary *dict = [jsonArray objectAtIndex:0];
NSDictionary *dict2 = [jsonArray objectAtIndex:1];
NSLog(@"%@",[dict valueForKey:@"people1"]);
NSLog(@"%@",[dict valueForKey:@"total1"]);
NSLog(@"%@",[dict2 valueForKey:@"people2"]);
NSLog(@"%@",[dict2 valueForKey:@"total2"]);