即時通訊解析JSON和即時通訊嘗試一個簡單的任務,以從預測天氣JSON文件中檢索URL。如何json解析與組件分隔字符串
在這裏,我解析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"
}
]
}
}
對不起我的英文不好,並請糾正我,如果即時通訊這樣做不對,提前感謝
顯示你在哪裏打電話componentsSeparatedByString –
你能後導致你從服務器檢索的JSON的代碼?你也不能在'NSArray'對象上使用'componentSeparatedByString:'它是'NSString'的一個方法。 – rckoenes
是的,使用JSONParser,並且不要調用NSArray對象上的componentsSeparatedByString,顯然它會崩潰,因爲NSArray沒有任何方法稱爲componentsSeparatedByString –