2012-05-07 162 views
0

我需要解析Apple JSON,但我有一個小問題。現在我這樣做:JSON解析返回null

的問題是在這裏:

- (void)fetchedData:(NSData *)responseData { 
    NSError* error; 
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData 
                 options:kNilOptions 
                  error:&error]; 



    NSNumber *resultCount = [json objectForKey:@"resultCount"]; 
    NSLog(@"%i", [resultCount intValue]); 

    NSArray * AppStoreUrlParse = [json objectForKey:@"results"]; 

    NSDictionary* StoreParse = [AppStoreUrlParse objectAtIndex:0]; 

    NSLog(@"%@", [json objectForKey:@"price"]); 

} 

在此行中:

NSLog(@"%i", [resultCount intValue]); 

的是NSLog的returing:1像JSON(http://itunes.apple.com/lookup?id=387633954

但是在這條線上

NSLog(@"%@", [json objectForKey:@"price"]); 

NSLOG返回(空)

沒有人知道我可以從json得到價格嗎?

回答

4

似乎「價格」是字典的一部分,它是'results'數組中的第一項。

你可以嘗試

NSLog(@"%@", [[AppStoreUrlParse objectAtIndex:0] objectForKey:@"price"]); 

這會得到在「結果」陣列的JSON的第一個元素的關鍵「價格」的價值。

編輯

其實,重讀你的代碼,你已經獲得該行的數組的第一個元素:

NSDictionary* StoreParse = [AppStoreUrlParse objectAtIndex:0]; 

因此,所有你需要做的是改變由StoreParsejson在你的NSLog:

NSLog(@"%@", [StoreParse objectForKey:@"price"]); 
+0

對我的作品感謝,會在4分鐘內接受你的回答 – Jones