Several Methods for parsing iOS json: NSJSONSerialization, JSONKit, SBJson, TouchJson, and jsonkitsbjson

Source: Internet
Author: User

Several Methods for parsing iOS json: NSJSONSerialization, JSONKit, SBJson, TouchJson, and jsonkitsbjson

You can download related third-party class libraries from github.

1. NSJSONSerialization

The Code is as follows:

-(Void) viewDidLoad {[super viewDidLoad]; NSData * data1 = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "test" ofType: @ "json"]; // 1. NSLog (@ "% @", data1); _ autoreleasing NSError * err; NSDictionary * dic = [NSJSONSerialization JSONObjectWithData: data1 options: nsjsonreadingmutablaves error: & err]; NSLog (@ "% @", dic [@ "user"] [@ "location"]); NSLog (@ "% @", dic );}
2. JSONKit. This is the third-party class library to be imported.
 [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 also needs to import third-party class libraries
- (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 third-party class library
- (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);    }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.