2013-05-14 41 views
-1

我正在iOS應用程序中下載並解析JSON訂閱源。 JSON訂閱源提供的某些數據部分在我的iOS應用程序中正確顯示,但JSON訂閱源的某些部分似乎返回「null」,即使在實際的JSON訂閱源本身中它們不包含數據「null」。iOS從JSON訂閱源返回「null」數據

這裏是我的代碼:

NSError *myError = nil; 
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&myError]; 
NSArray *results = [res objectForKey:@"current_observation"]; 
NSArray *cur = [results valueForKey:@"weather"]; 
NSArray *loc = [results valueForKey:@"full"]; 
NSArray *tmp = [results valueForKey:tmptype]; 

JSON供稿我想負荷是這個:

:JSON的飼料,返回 「空」 是 http://api.wunderground.com/api/595007cb79ada1b1/conditions/q/CA/San_Francisco.json

一部分

"full":"San Francisco, CA" 

上面返回「null」....爲什麼?我該如何解決這個問題?

感謝,丹

+0

「上面的返回」null「」 - 你上面的是一個關鍵值對。在什麼意義上它可以「返回null」(無論這意味着什麼)? – 2013-05-14 13:40:11

+0

對不起,我的解釋差,我的意思是,當我嘗試通過我的iOS應用程序訪問「完整」包含的數據時,我的應用程序返回(顯示)字符串「null」而不是「San Francisco,CA」。 – Supertecnoboff 2013-05-14 13:41:49

+0

你如何訪問數據? – 2013-05-14 13:43:37

回答

1

你需要

NSString *full= [[results valueForKey:@"display_location"] valueForKey:@"full"]]; 

,因爲它是一個嵌套的JSON。

+0

謝謝,謝謝,謝謝!!!!!!對,我對JSON非常陌生,所以非常感謝,現在我明白瞭如何使用嵌套的JSON數據。 – Supertecnoboff 2013-05-14 13:58:04

+0

很高興幫助隊友。 – Bonnie 2013-05-14 13:58:35

+1

像魅力一樣工作,並幫助解決了我使用嵌套JSON時遇到的所有其他JSON問題。 :D – Supertecnoboff 2013-05-14 14:00:27

1

首先,結果是NSArray類型的不行。

當您嘗試獲取密鑰的值時:full您沒有找到正確的路徑。 你在current_observation.full,但你想要的鑰匙是在current_observation.display_location.full。由於JSON嵌套,你需要進入正確的路徑。

你可以得到完整的路徑一氣呵成:

NSError *myError = nil; 
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&myError]; 
NSString *weather = [res valueForKeyPath:@"current_observation.weather"]; 
NSString *cur = [res valueForKeyPath:@"current_observation.display_location.full"]; 
0
NSError *myError = nil; 
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&myError]; 
NSArray *results = [res objectForKey:@"current_observation"]; 
NSArray *cur = [results valueForKey:@"weather"]; 
NSString *full= [[res valueForKey:@"display_location"] valueForKey:@"full"]]; 
NSArray *tmp = [results valueForKey:tmptype]; 

試試這可能對你有幫助。