现在的app开发很少有用到XML解析的了,主流的则是JSON。
1 // 2 // ViewController.m 3 // CX-JSON解析(三方JSONKit-master) 4 5 #import "ViewController.h" 6 #import "JSONKit.h" 7 @interface ViewController () 8 9 10 @end 11 12 @implementation ViewController 13 14 - (void)viewDidLoad { 15 [super viewDidLoad]; 16 17 NSString * urlString = @"http://localhost/city.json"; 18 19 NSURL * url = [NSURL URLWithString:urlString]; 20 21 NSURLRequest * request = [[NSURLRequest alloc]initWithURL:url cachePolicy:0 timeoutInterval:15]; 22 23 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { 24 //重点是这行代码,理解就可以,这个三方很好用。 25 NSDictionary * dict = [data objectFromJSONData]; 26 27 NSArray * array = [dict objectForKey:@"city"]; 28 29 for (NSDictionary * dictTwo in array) { 30 31 NSLog(@"%@",dictTwo); 32 33 } 34 35 36 }]; 37 38 } 39 /* 40 一部分结果 41 2016-03-18 19:53:11.441 CX-JSON解析(三方JSONKit-master)[6130:389488] { 42 city = ( 43 { 44 "city_id" = 1; 45 "city_name" = "U897fU57ceU533a"; 46 }, 47 { 48 "city_id" = 2; 49 "city_name" = "U671dU9633U533a"; 50 }, 51 { 52 "city_id" = 3; 53 "city_name" = "U6d77U6dc0U533a"; 54 }, 55 { 56 "city_id" = 4; 57 "city_name" = "U4e30U53f0U533a"; 58 }, 59 { 60 "city_id" = 5; 61 "city_name" = "U77f3U666fU5c71U533a"; 62 }, 63 { 64 "city_id" = 6; 65 "city_name" = "U901aU5ddeU533a"; 66 }, 67 { 68 "city_id" = 7; 69 "city_name" = "U987aU4e49U533a"; 70 }, 71 { 72 "city_id" = 8; 73 "city_name" = "U623fU5c71U533a"; 74 }, 75 { 76 "city_id" = 9; 77 "city_name" = "U5927U5174U533a"; 78 }, 79 { 80 "city_id" = 10; 81 "city_name" = "U660cU5e73U533a"; 82 }, 83 { 84 "city_id" = 11; 85 "city_name" = "U6000U67d4U533a"; 86 }, 87 { 88 "city_id" = 12; 89 "city_name" = "U5e73U8c37U533a"; 90 }, 91 { 92 "city_id" = 13; 93 "city_name" = "U95e8U5934U6c9fU533a"; 94 }, 95 { 96 "city_id" = 14; 97 "city_name" = "U5bc6U4e91U53bf"; 98 }, 99 100 */ 101 102 @end