您的位置:首页 > Web前端 > JavaScript

解析自定义的json数据

2015-09-07 14:07 501 查看
Json是开发中常见的数据交换格式,对于一般的json数据,使用NSJSONSerialization完全够用,代码如下:

/**
*  Json的数据表如下

[{
"name":"Vincent",
"age":"18",
"tel":{
"home":"123",
"comp":"456"
}
},{

"name":"Zander",
"age":"21",
"tel":{
"home":"789",
"comp":"568"
}
}]

*/
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
NSString *path=[[NSBundle mainBundle]pathForResource:@"info" ofType:@"json"];
NSData *data=[NSData dataWithContentsOfFile:path];
//解析json字符串
NSArray *jsonArr=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//循环输出后的解析结果
for (NSDictionary *dicty in jsonArr) {
NSString *name=[dicty valueForKey:@"name"];
NSString *age=[dicty valueForKey:@"age"];
NSDictionary *telDict=[dicty valueForKey:@"tel"];
NSString *homeTel=[telDict valueForKey:@"home"];
NSString *compTel=[telDict valueForKey:@"comp"];
NSLog(@"姓名:%@---年龄:%@---家庭电话:%@---公司电话:%@",name,age,homeTel,compTel);
}
// Do any additional setup after loading the view, typically from a nib.
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: