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); }