从JSON检索嵌套对象 - 目标C.

时间:2014-11-21 05:24:45

标签: objective-c json nsdata nsurl

通过嵌套的JSON结果解析我似乎遇到了一个小问题。如果JSON没有嵌套,则以下代码可以正常工作。我对于如何继续进行困惑感到困惑,因为每次尝试(通过其他人的例子)都失败了。

所以,为了测试这个,我使用了https://developer.worldweatheronline.com/page/explorer-free

中的以下API

我只想获得当前的温度(temp_c)。

以下是调用该服务的代码。请注意,我有一个将填充数据的NSObject,但当然我似乎无法进入那个阶段。它也是一个NSMutableArray。同样,我认为这不是问题,而是提供背景。

-(void)retrieveLocalWeatherService {
NSURL *url = [NSURL URLWithString:getLocalWeather];
NSData *data = [NSData dataWithContentsOfURL:url];

jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

//set up array and json call
weatherArray = [[NSMutableArray alloc]init];

//Loop through the JSON array
for (int i = 0; i< jsonArray.count; i++)
{
    //create our object
    NSString *nTemp = [[jsonArray objectAtIndex:i]objectForKey:@"temp_C"];
    NSString *nPressure = [[jsonArray objectAtIndex:i]objectForKey:@"pressure"];
    NSString *nHumidity = [[jsonArray objectAtIndex:i]objectForKey:@"humidity"];

    //Add the object to our animal array
    [weatherArray addObject:[[LocalWeather alloc]initWithtemp:(nTemp) andpressure:nPressure andhumidity:nHumidity]];
}

这是JSON响应。

{
"data": {
    "current_condition": [
        {
            "cloudcover": "75",
            "FeelsLikeC": "31",
            "FeelsLikeF": "88",
            "humidity": "70",
            "observation_time": "05:15 AM",
            "precipMM": "0.0",
            "pressure": "1011",
            "temp_C": "28",
            "temp_F": "82",
            "visibility": "10",
            "weatherCode": "116",
            "weatherDesc": [
                {
                    "value": "Partly Cloudy"
                }
            ],
            "weatherIconUrl": [
                {
                    "value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png"
                }
            ],
            "winddir16Point": "N",
            "winddirDegree": "10",
            "windspeedKmph": "41",
            "windspeedMiles": "26"
        }
    ],
    "request": [
        {
            "query": "Brisbane, Australia",
            "type": "City"
        }
    ],

我切断了JSON服务,因为它走了几英里,所以我哪里出错了?我相信它在“for-loop”中的某个地方,但不确定在哪里。我知道它的主要节点是“数据”,然后子节点是“current_condition”。我应该挖掘JSON结果吗?如果什么是最好的方法。

顺便说一下,我从服务器得到了一个包含整个JSON结果的响应......显然我是一个解析问题。

提前致谢! 请善待我是新手。

1 个答案:

答案 0 :(得分:1)

您正在以错误的方式解析JSON数据,您正在将JSON直接解析为Array但是根据您的JSON格式,您的JSON将返回NSDictionary而不是NSArray。

-(void)retrieveLocalWeatherService {
        NSURL *url = [NSURL URLWithString:getLocalWeather];
        NSData *data = [NSData dataWithContentsOfURL:url];

        NSDictionary *weatherJson = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

        NSArray *currentConditionArray = [weatherJson valueForKeyPath:@"Data.current_condition"];

        //set up array and json call
        weatherArray = [[NSMutableArray alloc]init];

        //Loop through the JSON array
        for (NSDictionary *item in currentConditionArray)
        {
            //create our object
            NSString *nTemp = [item objectForKey:@"temp_C"];
            NSString *nPressure = [item objectForKey:@"pressure"];
            NSString *nHumidity = [item  objectForKey:@"humidity"];

            //Add the object to our animal array
            [weatherArray addObject:[[LocalWeather alloc]initWithtemp:(nTemp) andpressure:nPressure andhumidity:nHumidity]];
        }
    }