【问题标题】:Parse world weather online JSON with ObjectiveC for iOS 6 [closed]使用 ObjectiveC for iOS 6 解析世界天气在线 JSON [关闭]
【发布时间】:2013-05-04 20:12:07
【问题描述】:

我需要一个建议:我从 http://www.worldweatheronline.com/free-weather.aspx 下载了一个 JSON。我有问题解析这个 JSON:

{
   "data":{
      "current_condition":[
         {
            "cloudcover":"0",
            "humidity":"57",
            "observation_time":"07:23 PM",
            "precipMM":"0.0",
            "pressure":"1013",
            "temp_C":"23",
            "temp_F":"73",
            "visibility":"10",
            "weatherCode":"113",
            "weatherDesc":[
               {
                  "value":"Clear"
               }
            ],
            "weatherIconUrl":[
               {
                  "value":"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0008_clear_sky_night.png"
               }
            ],
            "winddir16Point":"W",
            "winddirDegree":"275",
            "windspeedKmph":"0",
            "windspeedMiles":"0"
         }
      ],
      "request":[
         {
            "query":"Roma, Italy",
            "type":"City"
         }
      ],
      "weather":[
         {
            "date":"2013-05-04",
            "precipMM":"0.0",
            "tempMaxC":"26",
            "tempMaxF":"78",
            "tempMinC":"13",
            "tempMinF":"55",
            "weatherCode":"113",
            "weatherDesc":[
               {
                  "value":"Sunny"
               }
            ],
            "weatherIconUrl":[
               {
                  "value":"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png"
               }
            ],
            "winddir16Point":"WSW",
            "winddirDegree":"251",
            "winddirection":"WSW",
            "windspeedKmph":"9",
            "windspeedMiles":"6"
         }
      ]
   }
}

从这个 JSON 中我需要 3 个信息:tempMaxC、tempMinC 和 weatherIconUrl --> 值。直到现在我做了这个方法:

- (void)wheaterDidFinish:(NSDictionary*)object {
    if (object) {
        NSDictionary *obj = [object objectForKey:@"data"];
        NSArray *firstZero = [obj objectForKey:@"weather"];
        NSLog(@"%@ e %d", firstZero, firstZero.count);

    }
}

我怎样才能获得我需要的信息?你能帮助我吗? 谢谢

【问题讨论】:

    标签: ios objective-c json parsing


    【解决方案1】:
    -(void)wheaterDidFinish:(NSDictionary*)object 
    {
        NSString *tempMaxC=@"" , tempMinC =@"" , weatherIconUrl =@"";
    
        if (object) 
        {
             NSDictionary *obj = [object objectForKey:@"data"];
    
             NSArray *firstZero = [obj objectForKey:@"weather"];
    
             NSMutableDictionary *weatherDict = [firstZero objectAtIndex:0];
    
             tempMaxc = [NSString stringWithFormat:@"%@",[weatherDict objectForKey:@"tempMaxC"]];
    
             tempMinC = [NSString stringWithFormat:@"%@",[weatherDict objectForKey:@"tempMinC"]];
    
             NSArray *weatherIconArray=[weatherDict objectForKey:@"weatherIconUrl"];
    
             weatherIconUrl = [NSString stringWithFormat:@"%@",[[weatherIconArray objectAtIndex:0] objectForKey:@"value"]];
    
        }
    }
    

    这会解决你的问题

    【讨论】:

    • 谢谢!我试过了,效果很好!
    猜你喜欢
    • 2017-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-24
    • 1970-01-01
    • 2023-03-19
    • 2011-09-18
    • 1970-01-01
    相关资源
    最近更新 更多