zoukankan      html  css  js  c++  java
  • 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);
        
    }
    
  • 相关阅读:
    一起来学Java注解(Annotation)
    Intellij IDEA在maven项目中添加外部Jar包运行
    Java反射Reflect的使用详解
    Java泛型使用的简单介绍
    聊一聊Java的枚举enum
    Java集合 HashSet的原理及常用方法
    对比分析HashMap、LinkedHashMap、TreeMap
    TreeMap原理实现及常用方法
    关于红黑树(R-B tree)原理,看这篇如何
    百度地图-中国地图
  • 原文地址:https://www.cnblogs.com/qianLL/p/5329469.html
Copyright © 2011-2022 走看看