ios - 将 json 值分配给标签

标签 ios objective-c json

这里我有一些 json 代码。有 2 个 data.like

name1
name2
lap1
lap2

只有label1,label2显示值。 Label3,label4 没有显示值!

最佳答案

使用这个

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 个键的字典

关于ios - 将 json 值分配给标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34065954/

相关文章:

ios - 我可以使用 BLE 将 Iphone 4s 连接到最多 3 个设备(温度传感器)吗

ios - 为什么我不能在 Swift 中调用 UIViewController 上的默认 super.init()?

json - 如何使用 Angular JS 绑定(bind)从 @symbol 开始的 JSON 键

javascript - 如何只选择一些 JSON 对象?

objective-c - SoundcloudAPI Wrapper iOS5 身份验证错误

javascript - 如何从 URL 中提取 JSON 数据

ios - 具有自定义 View 和 ImageView 的 MKAnnotationView

iphone - FBLoginView iPhone View

ios - UITableViewCell 中的 UIDatePicker 更新 UITableViewCell 文本标签

ios - 如何在 "MyClassName.var"之类的 objective-c 中使用类变量?