Summary: JSON data parsing can use Apple's own nsjsonserialization, or use a third-party framework, such as Jsonkit, Sbjson, Touchjson, and so on, the framework is relatively simple to use, but the performance is much worse than the native way of iOS, It is recommended to use native mode.
1 Nsjsonserialization,ios native
/** * Parse JSON data */ -(void) analysisjson{ Get URL Nsurl *url = [[Nsurl alloc] initwithstring:@ "Http://localhost:8070/MJServer/video"]; Get an instance of session Nsurlsession *session = [Nsurlsession sharedsession]; Create a data Access task Nsurlsessiondatatask *task = [Session Datataskwithurl:url completionhandler:^ (NSData *data, Nsurlresponse *response, Nserror *error) { If an access error if (Error | | data = nil) { NSLog (@ "network busy, please try again later"); Return } Parse JSON data to know the structure of JSON data in advance Nsdictionary *result = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutableleaves Error:nil]; Nsarray *videos = result[@ "Videos"]; For (nsdictionary *dict in videos) { NSLog (@ "videoid-%@", dict[@ "id"]); NSLog (@ "Videoname-%@", dict[@ "name"]); NSLog (@ "Videourl-%@", dict[@ "url"]); printf ("\ n----------------gorgeous split line-----------------\ n"); } }]; [Task resume]; } |
2 Jsonkit, need to import #import "JSONKit.h", and to add libz.dylib dynamic Library
/** * Jsonkit */ -(ibaction) Jsonkit { NSLog (@ "Jsonkit"); Nsdictionary *weatherinfo = [[self weatherdata] objectfromjsondata]; Self.jsonData.text = [self weatherformat:weatherinfo]; } |
3 Sbjson, need to import #import "Sbjson/sbjson.h"
| /** * sbjson */ -(ibaction) Sbjson { nslog (@ "Sbjson"); nsurl *url = [[nsurl alloc] initwithstring:@] http://www.weather.com.cn/data/ Cityinfo/101010100.html "]; nsstring *responsestring = [nsstring stringwithcontentsofurl:url encoding: Nsutf8stringencoding error:nil]; // Create Sbjson Analysis objects sbjsonparser *parser = [[Sbjsonparser alloc] init]; // get a dictionary from JSON data nsdictionary *weatherinfo = [parser objectwithstring:responsestring]; self.jsondata.text = [self weatherformat:weatherinfo]; } |
4 Touchjson, need to import #import "Touchjson/json/cjsondeserializer.h"
/** * Touchjson */ -(ibaction) Touchjson { NSLog (@ "Touchjson"); Nsdictionary *weatherinfo = [[Cjsondeserializer deserializer]deserialize:[self Weatherdata] error:nil]; Self.jsonData.text = [self weatherformat:weatherinfo]; } |
2\3\4 take the weather information for example, its data acquisition method and format method are as follows
/** * Get weather information * { "Weatherinfo": { "City": "Beijing", "Cityid": "101010100", "Temp1": "12 ℃", "Temp2": "15 ℃", "Weather": "Light Rain", "IMG1": "N7.gif", "Img2": "D7.gif", "Ptime": "18:00" } } * @return JSON data */ -(NSData *) weatherdata{ Nsurl *url = [[Nsurl alloc] initwithstring:@ "http://www.weather.com.cn/data/cityinfo/101010100.html"]; Nsurlrequest *request = [Nsurlrequest Requestwithurl:url]; NSData *data = [nsurlconnection sendsynchronousrequest:request returningresponse:nil Error:nil]; Nsdictionary *weather = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments Error:nil]; NSLog (@ "Weather-%@", weather[@ "Weatherinfo"]); return data; } Weather formatting -(NSString *) Weatherformat: (Nsdictionary *) weatherinfo{ Nsdictionary *weather = weatherinfo[@ "Weatherinfo"]; NSString *city =weather[@ "City"]; NSString *status =weather[@ "Weather"]; NSString *temp1 =weather[@ "Temp1"]; NSString *temp2 =weather[@ "Temp2"]; NSString *ptime =weather[@ "Ptime"]; return [NSString stringwithformat:@] Weather forecast (%@ release) \n%@,%@, Temperature%@-%@ ", Ptime, city, status, Temp1, Temp2]; } |
iOS development uses nsjsonserialization, Jsonkit, Sbjson, Touchjson four ways to parse JSON data