解析自定义的json数据

来源:互联网 发布:菜鸟网络创始人 编辑:程序博客网 时间:2024/05/01 21:16

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.}


0 0
原创粉丝点击