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