使用NSJSONSerialization来解析JSON
关于这一点的讨论太多了,但我无法弄清楚如何解决我的问题。
这是我从WorldWeatherOnline获得的JSON数据。 JSON有效。 但我无法弄清楚如何解析它。 这是我的代码,后面是JSON。 请帮忙!
NSError* errorInfo; NSDictionary *parsedJSON = [NSJSONSerialization JSONObjectWithData:self.wwoWeatherData options:kNilOptions error:&errorInfo]; NSArray* temp = [parsedJSON objectForKey:@"temp_C"]; NSLog(@"%@", temp);
{ “数据”:{ “当前的状况”:[ { “cloudcover”: “0”, “湿度”: “82”, “observation_time”:“晚上11:07”, “precipMM”: “0.0”, “压力”: “1024”, “temp_C”: “16”, “temp_F”: “61”, “可见性”: “10”, “weatherCode”: “113”, “weatherDesc”: { “值”:“清除” } ] “weatherIconUrl”: { “值”: “HTTP:\ / \ / www.worldweatheronline.com \ /图像\ / wsymbols01_png_64 \ /wsymbol_0008_clear_sky_night.png” } ] “winddir16Point”: “北北东”, “winddirDegree”: “30”, “windspeedKmph”: “11”, “windspeedMiles”: “7” } ] “请求”:[ { “查询”:“Lat 48.85和Lon 2.35”, “类型”: “LatLon” } ] “天气”:[ { “日期”:“2012年9月4日” “precipMM”: “0.0”, “tempMaxC”: “25”, “tempMaxF”: “77”, “tempMinC”: “14”, “tempMinF”: “57”, “weatherCode”: “113”, “weatherDesc”: { “值”:“艳阳天” } ] “weatherIconUrl”: { “值”: “HTTP:\ / \ / www.worldweatheronline.com \ /图像\ / wsymbols01_png_64 \ /wsymbol_0001_sunny.png” } ] “winddir16Point”: “N”, “winddirDegree”: “5”, “winddirection”: “N”, “windspeedKmph”: “13”, “windspeedMiles”: “8” }, { “日期”: “2012-09-05”, “precipMM”: “0.0”, “tempMaxC”: “22”, “tempMaxF”: “72”, “tempMinC”: “10”, “tempMinF”: “50”, “weatherCode”: “113”, “weatherDesc”: { “值”:“艳阳天” } ] “weatherIconUrl”: { “值”: “HTTP:\ / \ / www.worldweatheronline.com \ /图像\ / wsymbols01_png_64 \ /wsymbol_0001_sunny.png” } ] “winddir16Point”: “北北东”, “winddirDegree”: “25”, “winddirection”: “北北东”, “windspeedKmph”: “20” “windspeedMiles”: “13” }, { “日期”: “2012-09-06”, “precipMM”: “0.0”, “tempMaxC”: “22”, “tempMaxF”: “71”, “tempMinC”: “11”, “tempMinF”: “51”, “weatherCode”: “113”, “weatherDesc”: { “值”:“艳阳天” } ] “weatherIconUrl”: { “值”: “HTTP:\ / \ / www.worldweatheronline.com \ /图像\ / wsymbols01_png_64 \ /wsymbol_0001_sunny.png” } ] “winddir16Point”: “NE” “winddirDegree”: “42”, “winddirection”: “NE” “windspeedKmph”: “15”, “windspeedMiles”: “10” }, { “日期”:“2012年9月7日” “precipMM”: “0.0”, “tempMaxC”: “24”, “tempMaxF”: “75”, “tempMinC”: “13”, “tempMinF”: “55”, “weatherCode”: “116”, “weatherDesc”: { “价值”:“部分多云” } ] “weatherIconUrl”: { “值”: “HTTP:\ / \ / www.worldweatheronline.com \ /图像\ / wsymbols01_png_64 \ /wsymbol_0002_sunny_intervals.png” } ] “winddir16Point”: “ENE” “winddirDegree”: “56”, “winddirection”: “ENE” “windspeedKmph”: “13”, “windspeedMiles”: “8” }, { “日期”:“2012年9月8日” “precipMM”: “0.0”, “tempMaxC”: “26”, “tempMaxF”: “78”, “tempMinC”: “16”, “tempMinF”: “61”, “weatherCode”: “113”, “weatherDesc”: { “值”:“艳阳天” } ] “weatherIconUrl”: { “值”: “HTTP:\ / \ / www.worldweatheronline.com \ /图像\ / wsymbols01_png_64 \ /wsymbol_0001_sunny.png” } ] “winddir16Point”: “ENE” “winddirDegree”: “76”, “winddirection”: “ENE” “windspeedKmph”: “9”, “windspeedMiles”: “6” } ] } }
您解析的json包含一个名为data
的字典。 在该字典中是current_condition
的数组。 继续向下钻取数据结构以查找您要查找的属性:
NSDictionary *data = [parsedJSON objectForKey:@"data"]; NSArray *currentConditions = [data objectForKey:@"current_condition"]; NSDictionary *condition = [currentConditions objectAtIndex:0]; NSString *tempC = [condition objectForKey:@"temp_C"];