(1) UseTouchJSonResolution Method: (the package needs to be imported: # import TouchJson/JSON/CJSONDeserializer. h)
// Use TouchJson to parse the weather in Beijing-(IBAction) btnPressTouchJson :( id) sender {// obtain the api nsurl * url = [NSURL URLWithString: @ override // defines an NSError object to capture the error message NSError * error; NSString * jsonString = [NSString stringWithContentsOfURL: url encoding: NSUTF8StringEncoding error: & error]; NSLog (@ jsonString ---> % @, jsonString); // store the parsed content in the dictionary. The encoding format is UTF8, prevent garbled NSDictionary * rootDic = [[CJSONDeserializer deserializer] deserialize: [jsonString dataUsingEncoding: NSUTF8StringEncoding] error: & error]; // The returned Json file has two layers, go to the second layer and put the content in the dictionary to NSDictionary * weatherInfo = [rootDic objectForKey: @ weatherinfo]; NSLog (@ weatherInfo --- >%@, weatherInfo); // print txtView with the value. text = [NSString stringWithFormat: @ today is %%%%%@ the weather condition is: %%%@, [weatherInfo objectForKey: @ date_y], [weatherInfo objectForKey: @ week], [weatherInfo objectForKey: @ city], [weatherInfo objectForKey: @ weather1], [weatherInfo objectForKey: @ temp1];}(2) Use
SBJsonResolution Method: (the package needs to be imported: # import SBJson/SBJson. h)
// Use SBJson to parse the weather condition (IBAction) btnPressSBJson :( id) sender {NSURL * url = [NSURL URLWithString: @ brief NSError * error = nil; NSString * jsonString = [NSString stringWithContentsOfURL: url encoding: Unknown error: & error]; SBJsonParser * parser = [[SBJsonParser alloc] init]; NSDictionary * rootDic = [parser objectWithString: jsonString error: & error]; NSDictionary * weatherInfo = [rootDic objectForKey: @ weatherinfo]; txtView. text = [NSString stringWithFormat: @ today is %%%%%@ the weather condition is: %%%@, [weatherInfo objectForKey: @ date_y], [weatherInfo objectForKey: @ week], [weatherInfo objectForKey: @ city], [weatherInfo objectForKey: @ weather1], [weatherInfo objectForKey: @ temp1];}(3) Use
Built-in parsing class NSJSONSerialization for IOS5Method resolution: (no package needs to be imported, supported by IOS5, not supported by IOS in earlier versions)
-(IBAction) btnPressIOS5Json :( id) sender {NSError * error; // load an NSURL object NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL URLWithString: @ http://m.weather.com.cn/data/101180601.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]; txtView. text = [NSString stringWithFormat: @ today is %%%%%@ the weather condition is: %%%@, [weatherInfo objectForKey: @ date_y], [weatherInfo objectForKey: @ week], [weatherInfo objectForKey: @ city], [weatherInfo objectForKey: @ weather1], [weatherInfo objectForKey: @ temp1]; NSLog (@ weatherInfo the content in the dictionary is -- "% @, weatherDic );}(4) use
JSONKit(Import the package: # import JSONKit/JSONKit. h)
-(Void) btnPressJsonKit :( id) sender {// If json is a "single layer", that is, the values are strings and numbers, you can use objectFromJSONString NSString * json1 =@{ a: 123,: abc}; NSLog (@ json1: % @, json1); NSDictionary * data1 = [json1 objectFromJSONString]; NSLog (@ json1.a: % @, [data1 objectForKey: @ a]); NSLog (@ json1. B: % @, [data1 objectForKey: @ B]); [json1 release]; // If json is nested, that is, there are arrays and objects in the value. If objectFromJSONString is used again, the program may report an error // (the test result shows that an error is reported when the json file generated by the php/json_encode generated by the network is used, but when using the json string defined by NSString, the resolution is successful.) it is best to use objectFromJSONStringWithParseOptions: NSString * json2 =@{ a: 123,: abc, c: [456, hello], d: {ame: James, age :}}; NSLog (@ json2: % @, json2); NSDictionary * data2 = [json2 UNICODE: JKParseOptionLooseUnicode]; NSLog (@ json2.c: % @, [data2 objectForKey: @ c]); NSLog (@ json2.d: % @, [data2 objectForKey: @ d]); [json2 release];}