iphone - 使用 NSJSONSerialization 解析 JSON

标签 iphone ios 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);
   {
   "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"
         }
      ]
   }
}

最佳答案

您解析的 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"];

关于iphone - 使用 NSJSONSerialization 解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12255290/

相关文章:

iphone - 如何保证核心数据实体的单一实例

android - 使用 Web App 的移动推送通知

ios - 尝试在一个屏幕上显示三张全景照片时应用程序崩溃

ios - 添加第一个项目时(以及删除最后一个项目时)tableView 崩溃

javascript - 问题 : can't access Json data inside the html template, AngularJS

iphone - 每个 tableview 组中的自定义单元格数量

ios - 获取 AdMob 中介 ID

ios - 如何删除 UITableView 部分标题删除后创建的空间?

javascript - IvoryCKEditorBundle uploadimage无法解析JSON响应

java - 解析有效 JSON 时出现 Volley org.json.JSONException