In the previous iOS study-xml data parsing (9), we introduced xml data parsing. This article briefly introduces Json data parsing. JSON is JavaScript Object Natation. It is a lightweight data exchange format and is suitable for server-to-client interaction. Json syntax reference. Many third-party open-source projects, such as TouchJson, JSONKit, and SBJon, have been involved in JSON parsing on the iOS platform. Since iOS5.0, Apple SDK has released its own JSON solution NSJSONSerialization, this is a very useful JSON generation and resolution tool, which is much more efficient than other third-party open-source projects. For details, see.
You can view the image details.
NSJSONSerialization provides Json Data encapsulation and Json data parsing. NSJSONSerialization converts JSON data to NSDictionary or NSArray to unpack data, converts NSDictionary and NSArray objects to JSON data (you can call isValidJSONObject to determine whether NSDictionary and NSArray objects can be converted to JSON data) packets. This article will give a brief introduction.
Json data packets
NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys: @ "value1", @ "key1", @ "value2", @ "key2", @ "value3", @ "key3 ", nil]; // isValidJSONObject determines whether the object can be constructed into a json object if ([NSJSONSerialization isValidJSONObject: dic]) {NSError * error; // create a json file from Data, NSJSONWritingPrettyPrinted: the white space of the specified JSON data to make the output more readable. NSData * jsonData = [NSJSONSerialization dataWithJSONObject: dic options: Internal error: & error]; NSString * json = [[NSString alloc] initWithData: jsonData encoding: NSUTF8StringEncoding]; NSLog (@ "json data: % @", json );}Json data parsing
NSError * error; // load an NSURL object NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL URLWithString: @ "http://m.weather.com.cn/data/101120101.html"]; // put the request url data in the NSData object NSData * response = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil]; // NSDictionary * weatherDic = [NSJSONSerialization JSONObjectWithData: response options: nsjsonreadingmutablaves error: & error]; NSDictionary * weatherInfo = [weatherDic objectForKey: @ "weatherinfo"]; NSString * text = [NSString stringWithFormat: @ "today's weather conditions are: % @ ", [weatherInfo objectForKey: @" date_y "], [weatherInfo objectForKey: @" week "], [weatherInfo objectForKey: @" city "], [weatherInfo objectForKey: @ "weather1"], [weatherInfo objectForKey: @ "temp1"]; NSLog (@ "weatherInfo: % @", text );
/**
* @ Author Zhang xingye * http://blog.csdn.net/xyz_lmn* iOS entry group: 83702688
* Android Development Group: 241395671
* My Sina Weibo:@ Zhang xingye tbow*/
Refer:
Http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40010946-CH1-DontLinkElementID_1
Http://www.w3school.com.cn/json/
Http://www.ibm.com/developerworks/cn/web/wa-lo-json/
Http://blog.sina.com.cn/s/blog_7018d3820101bdqz.html