2012-09-04 38 views
7

Quá nhiều cuộc thảo luận đã xảy ra về điều này nhưng tôi không thể tìm ra cách giải quyết vấn đề của mình.Sử dụng NSJSONSerialization để phân tích cú pháp JSON

Đây là dữ liệu JSON tôi nhận được từ WorldWeatherOnline. JSON là hợp lệ. Nhưng tôi không thể tìm ra cách phân tích nó. Đây là mã của tôi, tiếp theo là JSON. Hãy giúp tôi!

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" 
     } 
     ] 
    } 
} 

Trả lời

8

json của bạn mà bạn phân tích có chứa một từ điển tên data. Trong từ điển đó là một mảng của current_condition. Tiếp tục khoan qua các cấu trúc dữ liệu để tìm các thuộc tính bạn đang tìm kiếm:

NSDictionary *data = [parsedJSON objectForKey:@"data"]; 
NSArray *currentConditions = [data objectForKey:@"current_condition"]; 
NSDictionary *condition = [currentConditions objectAtIndex:0]; 
NSString *tempC = [condition objectForKey:@"temp_C"]; 
+0

Đây có phải là: NSLog (@ "% @", currentConditions); sau đó hiển thị các giá trị bên trong current_condition? Nếu có, thì nó không hoạt động. Nó hiển thị null. –

+0

xin lỗi, đã xảy ra lỗi sao chép/dán. dòng thứ hai sẽ lấy current_condition từ dữ liệu, không phân tích cú phápJson – highlycaffeinated

+0

Điều đó có ý nghĩa. Nhưng nếu bạn thấy truy vấn của tôi, tôi đang cố gắng để có được temp_c. Khi tôi in currentconditions, nó mang lại cho tôi tất cả các giá trị bên trong current_condition. Tôi có cần phải làm một ObjectAtIndex cho mảng currentconditions? Nếu vậy, tôi có thực sự cần phải mã hóa chỉ mục không? Tôi mới sử dụng mã hóa JSON và iOS, vì vậy hãy chịu đựng với tôi. –

Các vấn đề liên quan