使用AFNetworking从Open weather Map API解析JSON天气对象



我正在使用AFNetworking检索有关特定位置的天气信息,例如:

http://api.openweathermap.org/data/2.5/weather?q={New%20York%20City}

我正在使用AFNetworking框架,但我有问题解析JSON的一些对象。

如果我有一个NSDictionary,其中包含来自JSON的MAIN对象信息:

NSDictionay *main = [responseObject objectForKey:@"main"];

如果我记录主NSDictionary,我将得到以下有效输出:

"main":{  
      "temp":296.78;
      "pressure":1011;
      "humidity":69;
      "temp_min":293.15;
      "temp_max":299.82
   };

尽管如果我创建一个包含天气对象的NSDictionary,我将在将其记录到控制台时获得以下信息:

NSDictionay *weather = [responseObject objectForKey:@"weather"];
"weather":(  
      {  
         "id":801;
         "main":"Clouds";
         "description":"few clouds";
         "icon":"02d"
      }
   );

解析后的信息包含(括号而不是原始响应中的[)。这使我不能正确地访问weather对象的内部属性。

总而言之,我能够访问MAIN对象的所有内部变量,但是我不能访问Weather对象的属性(例如访问图标属性)。

有人能帮我一下吗?

谢谢你,

您调用的服务如下:

    NSString *query = @"http://api.openweathermap.org/data/2.5/weather?q={New%20York%20City}";
    NSLog(@"%@",query);
    query = [query stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSData *jsonData = [[NSString stringWithContentsOfURL:[NSURL URLWithString:query] encoding:NSUTF8StringEncoding error:nil] dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error = nil;
    NSDictionary *results = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error] : nil;

现在打印Response:

NSLog(@"weather==%@",[results objectForKey:@"weather"]);
NSLog(@"description==%@",[[[results objectForKey:@"weather"] objectAtIndex:0] objectForKey:@"description"]);
NSLog(@"icon==%@",[[[results objectForKey:@"weather"] objectAtIndex:0] objectForKey:@"icon"]);
NSLog(@"id==%@",[[[results objectForKey:@"weather"] objectAtIndex:0] objectForKey:@"id"]);
NSLog(@"main==%@",[[[results objectForKey:@"weather"] objectAtIndex:0] objectForKey:@"main"]);

你的回复是:

whwather==(
{
description = "sky is clear";
icon = 01d;
id= 800;
main= Clear;
}
)
description== "sky is clear";
icon == 01d;
id == 800;
main == Clear;

最新更新