पार्स करने के लिए NSJSONSerialization का उपयोग करके इस पर बहुत सी चर्चाएं हुई हैं और फिर भी मैं यह नहीं समझ सकता कि मेरी समस्या का समाधान कैसे करें।JSON
यहां वर्ल्डसदरऑनलाइन से प्राप्त JSON डेटा है। जेएसओएन मान्य है। लेकिन मैं यह समझ नहीं सकता कि इसे कैसे पार्स किया जाए। जेएसओएन के बाद यह मेरा कोड है। कृपया सहायता कीजिए!
NSError* errorInfo;
NSDictionary *parsedJSON = [NSJSONSerialization JSONObjectWithData:self.wwoWeatherData options:kNilOptions error:&errorInfo];
NSArray* temp = [parsedJSON objectForKey:@"temp_C"];
NSLog(@"%@", temp);
{ "data":{ "current_condition":[ { "cloudcover":"0", "humidity":"82", "observation_time":"11:07 PM", "precipMM":"0.0", "pressure":"1024", "temp_C":"16", "temp_F":"61", "visibility":"10", "weatherCode":"113", "weatherDesc":[ { "value":"Clear" } ], "weatherIconUrl":[ { "value":"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0008_clear_sky_night.png" } ], "winddir16Point":"NNE", "winddirDegree":"30", "windspeedKmph":"11", "windspeedMiles":"7" } ], "request":[ { "query":"Lat 48.85 and Lon 2.35", "type":"LatLon" } ], "weather":[ { "date":"2012-09-04", "precipMM":"0.0", "tempMaxC":"25", "tempMaxF":"77", "tempMinC":"14", "tempMinF":"57", "weatherCode":"113", "weatherDesc":[ { "value":"Sunny" } ], "weatherIconUrl":[ { "value":"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png" } ], "winddir16Point":"N", "winddirDegree":"5", "winddirection":"N", "windspeedKmph":"13", "windspeedMiles":"8" }, { "date":"2012-09-05", "precipMM":"0.0", "tempMaxC":"22", "tempMaxF":"72", "tempMinC":"10", "tempMinF":"50", "weatherCode":"113", "weatherDesc":[ { "value":"Sunny" } ], "weatherIconUrl":[ { "value":"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png" } ], "winddir16Point":"NNE", "winddirDegree":"25", "winddirection":"NNE", "windspeedKmph":"20", "windspeedMiles":"13" }, { "date":"2012-09-06", "precipMM":"0.0", "tempMaxC":"22", "tempMaxF":"71", "tempMinC":"11", "tempMinF":"51", "weatherCode":"113", "weatherDesc":[ { "value":"Sunny" } ], "weatherIconUrl":[ { "value":"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png" } ], "winddir16Point":"NE", "winddirDegree":"42", "winddirection":"NE", "windspeedKmph":"15", "windspeedMiles":"10" }, { "date":"2012-09-07", "precipMM":"0.0", "tempMaxC":"24", "tempMaxF":"75", "tempMinC":"13", "tempMinF":"55", "weatherCode":"116", "weatherDesc":[ { "value":"Partly Cloudy" } ], "weatherIconUrl":[ { "value":"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0002_sunny_intervals.png" } ], "winddir16Point":"ENE", "winddirDegree":"56", "winddirection":"ENE", "windspeedKmph":"13", "windspeedMiles":"8" }, { "date":"2012-09-08", "precipMM":"0.0", "tempMaxC":"26", "tempMaxF":"78", "tempMinC":"16", "tempMinF":"61", "weatherCode":"113", "weatherDesc":[ { "value":"Sunny" } ], "weatherIconUrl":[ { "value":"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png" } ], "winddir16Point":"ENE", "winddirDegree":"76", "winddirection":"ENE", "windspeedKmph":"9", "windspeedMiles":"6" } ] } }
यह होना चाहिए: एनएसएलओजी (@ "% @", वर्तमान कंडिशन); फिर current_condition के अंदर मूल्य दिखाओ? यदि हां, तो यह काम नहीं कर रहा है। यह शून्य दिखा रहा है। –
क्षमा करें, एक प्रतिलिपि/पेस्ट त्रुटि थी। दूसरी पंक्ति को डेटा से current_condition खींचना चाहिए, पार्सड जेसन – highlycaffeinated
यह समझ में आता है। लेकिन अगर आप मेरी क्वेरी देखते हैं, तो मैं temp_c प्राप्त करने की कोशिश कर रहा हूं। जब मैं वर्तमान शर्त प्रिंट करता हूं, तो यह मुझे current_condition के अंदर सभी मान देता है। क्या मुझे CurrentConditions सरणी के लिए ऑब्जेक्टएटइंडेक्स करने की ज़रूरत है? यदि हां, तो क्या मुझे वास्तव में इंडेक्स को हार्डकोड करने की आवश्यकता है? मैं जेएसओएन और आईओएस कोडिंग दोनों के लिए नया हूं, इसलिए कृपया मेरे साथ भालू। –