2012-09-04 104 views
7

太多的討論已經發生了這一點,但我無法弄清楚如何解決我的問題。使用NSJSONSerialization解析JSON

這裏的JSON數據,我從WorldWeatherOnline得到。 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" 
     } 
     ] 
    } 
} 

回答

8

你的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"]; 
+0

應該這樣:NSLog(@「%@」,currentConditions);然後顯示current_condition內的值?如果是,那麼它不起作用。它顯示爲空。 –

+0

對不起,有一個複製/粘貼錯誤。第二行應該從數據拉current_condition,不parsedJson – highlycaffeinated

+0

這是有道理的。但是,如果你看到我的查詢,我試圖得到temp_c。當我打印currentconditions,它給了我current_condition內的所有值。我需要爲currentconditions數組做一個ObjectAtIndex嗎?如果是這樣,我是否真的需要對索引進行硬編碼?我對JSON和iOS編碼都很陌生,所以請耐心等待。 –