2012-09-12 203 views
0

即時通訊解析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" 
      } 
     ] 
    } 
} 

對不起我的英文不好,並請糾正我,如果即時通訊這樣做不對,提前感謝

+0

顯示你在哪裏打電話componentsSeparatedByString –

+0

你能後導致你從服務器檢索的JSON的代碼?你也不能在'NSArray'對象上使用'componentSeparatedByString:'它是'NSString'的一個方法。 – rckoenes

+0

是的,使用JSONParser,並且不要調用NSArray對象上的componentsSeparatedByString,顯然它會崩潰,因爲NSArray沒有任何方法稱爲componentsSeparatedByString –

回答

2

如果你真的有周圍您的網址報價,然後嘗試這樣的事情:

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

這是來自服務器的響應:http://free.worldweatheronline.com /feed/weather.ashx?q=Fanzeres&format=json&num_of_days=2&key=8c1537f84d183203122108嘗試了你的方法並得到了 - [__ NSArrayI stringByTrimmingCharactersInSet:]:無法識別的選擇器發送到實例。感謝你目前的幫助。 – Bruno

+0

你知道你的json的結構嗎?然後得到你想要修剪的字符串。我認爲這些字符串是在那個陣列中,但他們在別的地方。我不太瞭解你如何設置你的結果數組,這使得我很難告訴你到底該怎麼做。但我在json中看不到額外的引號。你能解釋你是如何驗證這些報價的存在嗎? –

3

使用objectForKey,而不是從@「weatherIconUrl」獲取數組時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]; 
+0

[res objectForKey:@「weatherIconUrl」];感謝你目前的幫助。 – Bruno

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"]; 

所顯示的網址是在使用的NSURLRequest並顯示在網頁視圖

enter image description here