如何json解析组件Separated By String

时间:2012-09-12 09:35:33

标签: iphone ios json

我刚刚解析JSON并尝试一个简单的任务,从预测天气json文件中检索URL。

这里我解析json和i 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"
            }
        ]
    }
}

抱歉我的英文不好,如果我做错了,请纠正我,提前谢谢

3 个答案:

答案 0 :(得分:3)

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

NSString *weatherIconUrlString = [results2 objectAtIndex:0]

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

NSURL *url = [NSURL URLWithString:weatherIconUrlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url 
BOOL canGo = [NSURLConnection canHandleRequest:request];

答案 1 :(得分:2)

如果您的网址确实有引号,请尝试以下操作:

NSString *someURLString = [results2 objectAtIndex:0];
NSString *quotesRemoved = [someURLString stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\""]];

答案 2 :(得分:1)

通过jsonLint.com放置服务器的输出,可以更容易地读取json的格式。

现在,下面的代码会根据需要获取天气图标网址。它假定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

enter image description here