0

我是解析 JSON 的新手,我正在尝试一个简单的任务,即从天气预报 json 文件中检索 URL。

在这里,我解析 json 并 NSLog 数据的每个组件的内容:

NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves  error:&myError];

NSArray *data =  [res objectForKey:@"data"];
NSLog(@"data=%@",data);

NSArray *results =  [data valueForKey:@"weather"];
NSLog(@"weather=%@",results);

NSArray *results1 =  [results valueForKey:@"tempMaxC"];
NSLog(@"tempMaxC=%@",results1);

NSArray *results2 =  [results1 valueForKey:@"weatherIconUrl"];
NSLog(@"weatherIconUrl=%@",results2);

问题是当我得到 WeatherIconUrl 它带有这种格式

"http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"

而且我无法在没有引号的情况下获取 url,我尝试使用 nsrange 和 componentsSeparatedByString 但它总是给我这个错误:

[__NSArrayI componentsSeparatedByString:]: unrecognized selector sent to instance 

来自服务器的 JSON:

{
    "data": {
        "current_condition": [
            {
                "cloudcover": "0",
                "humidity": "73",
                "observation_time": "12:19 PM",
                "precipMM": "0.0",
                "pressure": "1021",
                "temp_C": "23",
                "temp_F": "73",
                "visibility": "10",
                "weatherCode": "113",
                "weatherDesc": [
                    {
                        "value": "Sunny"
                    }
                ],
                "weatherIconUrl": [
                    {
                        "value": "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
                    }
                ],
                "winddir16Point": "NW",
                "winddirDegree": "320",
                "windspeedKmph": "17",
                "windspeedMiles": "11"
            }
        ],
        "request": [
            {
                "query": "Fanzeres, Portugal",
                "type": "City"
            }
        ],
        "weather": [
            {
                "date": "2012-09-12",
                "precipMM": "0.0",
                "tempMaxC": "28",
                "tempMaxF": "83",
                "tempMinC": "17",
                "tempMinF": "63",
                "weatherCode": "113",
                "weatherDesc": [
                    {
                        "value": "Sunny"
                    }
                ],
                "weatherIconUrl": [
                    {
                        "value": "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
                    }
                ],
                "winddir16Point": "NW",
                "winddirDegree": "312",
                "winddirection": "NW",
                "windspeedKmph": "16",
                "windspeedMiles": "10"
            },
            {
                "date": "2012-09-13",
                "precipMM": "0.0",
                "tempMaxC": "33",
                "tempMaxF": "91",
                "tempMinC": "17",
                "tempMinF": "63",
                "weatherCode": "113",
                "weatherDesc": [
                    {
                        "value": "Sunny"
                    }
                ],
                "weatherIconUrl": [
                    {
                        "value": "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
                    }
                ],
                "winddir16Point": "N",
                "winddirDegree": "8",
                "winddirection": "N",
                "windspeedKmph": "10",
                "windspeedMiles": "6"
            }
        ]
    }
}

对不起我的英语不好,如果我做错了请纠正我,提前谢谢

4

3 回答 3

3

从@“weatherIconUrl”获取数组时使用objectForKey而不是valueForKey,然后将字符串放入NSString,例如

NSString *weatherIconUrlString = [results2 objectAtIndex:0]

要检查这是一个有效的 url,请使用 NSURLConnection 的 canHandleRequest 方法,例如

NSURL *url = [NSURL URLWithString:weatherIconUrlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url 
BOOL canGo = [NSURLConnection canHandleRequest:request];
于 2012-09-12T09:49:09.367 回答
2

如果您的 URL 周围确实有引号,请尝试以下操作:

NSString *someURLString = [results2 objectAtIndex:0];
NSString *quotesRemoved = [someURLString stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\""]];
于 2012-09-12T09:48:25.600 回答
1

将服务器的输出通过jsonLint.com提供更易于阅读的 json 格式。

下面的代码现在根据需要获取天气图标 url。它假定 json 已作为名为 jsonData 的 NSData 对象下载,并且不检查数据所指的日期。

NSError *error = nil;
NSDictionary *jsonDict      = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves
                                                                error:&error];
NSArray      *data          =  [jsonDict    valueForKey:@"data"];
NSArray      *weather       =  [data        valueForKey:@"weather"];
NSArray      *weatherIcon   = [[weather     objectAtIndex:0] valueForKey:@"weatherIconUrl"];
NSString     *url           = [[weatherIcon objectAtIndex:0] valueForKey:@"value"];

生成的 url 用于 NSURLRequest 并显示在 webview 中

在此处输入图像描述

于 2012-09-12T11:28:29.127 回答