IOS 网络浅析-(七 JSON解析之三方JSONKit)
在这个网络横行的时代.........
有没有小说的感觉,哈哈😄。
言归正传,之前我写过XML的网络解析,但是现在的app开发很少有用到XML解析的了,主流的则是JSON。(有时间我会对其进行总结)三方更是让json解析更加简便,由于代码过于简单,请大家做好心理准备。不要被吓到哦。
// // ViewController.m // CX-JSON解析(三方JSONKit-master) // // Created by ma c on 16/3/18. // Copyright © 2016年 xubaoaichiyu. All rights reserved. // #import "ViewController.h" #import "JSONKit.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSString * urlString = @"http://localhost/city.json"; NSURL * url = [NSURL URLWithString:urlString]; NSURLRequest * request = [[NSURLRequest alloc]initWithURL:url cachePolicy:0 timeoutInterval:15]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { //重点是这行代码,理解就可以,这个三方很好用。 NSDictionary * dict = [data objectFromJSONData]; NSArray * array = [dict objectForKey:@"city"]; for (NSDictionary * dictTwo in array) { NSLog(@"%@",dictTwo); } }]; } /* 一部分结果 2016-03-18 19:53:11.441 CX-JSON解析(三方JSONKit-master)[6130:389488] { city = ( { "city_id" = 1; "city_name" = "\U897f\U57ce\U533a"; }, { "city_id" = 2; "city_name" = "\U671d\U9633\U533a"; }, { "city_id" = 3; "city_name" = "\U6d77\U6dc0\U533a"; }, { "city_id" = 4; "city_name" = "\U4e30\U53f0\U533a"; }, { "city_id" = 5; "city_name" = "\U77f3\U666f\U5c71\U533a"; }, { "city_id" = 6; "city_name" = "\U901a\U5dde\U533a"; }, { "city_id" = 7; "city_name" = "\U987a\U4e49\U533a"; }, { "city_id" = 8; "city_name" = "\U623f\U5c71\U533a"; }, { "city_id" = 9; "city_name" = "\U5927\U5174\U533a"; }, { "city_id" = 10; "city_name" = "\U660c\U5e73\U533a"; }, { "city_id" = 11; "city_name" = "\U6000\U67d4\U533a"; }, { "city_id" = 12; "city_name" = "\U5e73\U8c37\U533a"; }, { "city_id" = 13; "city_name" = "\U95e8\U5934\U6c9f\U533a"; }, { "city_id" = 14; "city_name" = "\U5bc6\U4e91\U53bf"; }, */ @end