iOS json解析的几种方法 NSJSONSerialization,JSONKit,SBJson ,TouchJson

相关的第三方类库大家可以去github上下载

1.NSJSONSerialization

具体代码如下 :

- (void)viewDidLoad {
    [super viewDidLoad];
    NSData *data1=[NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"test" ofType:@"json"]];
    
    //1.系统提供的json解析方法
    NSLog(@"%@",data1);
    __autoreleasing NSError *err;
      NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data1 options:NSJSONReadingMutableLeaves error:&err];
    NSLog(@"%@",dic[@"user"][@"location"]);
    NSLog(@"%@",dic);
}

 2.JSONKit 这是需要导入第三方类库

 [super viewDidLoad];
    
    NSString *path=[[NSBundle mainBundle]pathForResource:@"sina.json" ofType:nil];
    
    NSData *data=[NSData dataWithContentsOfFile:path];
    
    
    __autoreleasing NSError *err;
    
    NSDictionary *dic=[data objectFromJSONDataWithParseOptions:JKParseOptionNone error:&err];
    
    NSLog(@"%@",dic[@"user"][@"location"]);

 

3.SBJson 同样需要导入第三方类库

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *path=[[NSBundle mainBundle]pathForResource:@"sina.json" ofType:nil];
    NSData *data=[NSData dataWithContentsOfFile:path];
    
    
    
    SBJsonParser *jsonParser=[[SBJsonParser alloc]init];
    

    NSMutableDictionary *dic=[jsonParser  objectWithData:data];

    NSLog(@"%@",dic);
    
}

 4.TouchJson 第三方类库

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSString *path=[[NSBundle mainBundle]pathForResource:@"sina" ofType:@"json"];
    
    NSData *data=[NSData dataWithContentsOfFile:path];
 
    NSDictionary *dic=[[CJSONDeserializer deserializer] deserialize:data error:nil];
    
    NSLog(@"%@",dic);
    
}

 

转载于:https://www.cnblogs.com/qianLL/p/5329469.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值