2014-11-21 107 views
0

我已經與Cheapshark API玩弄與此代碼想出了:Cheapshark API:閱讀NSDictionary的無鑰匙

- (IBAction)sendRequest:(UIButton *)sender { 

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.cheapshark.com/api/1.0/deals?storeID=6&desc=0&title=civilization%20V&pageSize=2"] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10]; 
    [request setHTTPMethod:@"GET"]; 

    NSError *requestError; 
    NSURLResponse *urlResponse = nil; 

    NSData *response1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError];   
    NSDictionary *results = [NSJSONSerialization JSONObjectWithData:response1 options:NSJSONReadingMutableContainers error:nil]; 
} 

望着調試器,我可以看到的NSDictionary結果有此值:

(lldb) po results 
<__NSArrayM 0x17004aaa0>(
{ 
    dealID = "exhfdIoNgaoB4ieTIZi30ObELpggqQ9B9hpkSnN5rl0%3D"; 
    dealRating = "0.0"; 
    gameID = 106353; 
    internalName = SIDMEIERSCIVILIZATIONVTHECOMPLETEEDITION; 
    lastChange = 1412962351; 
    metacriticLink = "/game/pc/sid-meiers-civilization-v-the-complete-edition"; 
    metacriticScore = 0; 
    normalPrice = "49.99"; 
    releaseDate = 1392076800; 
    salePrice = "49.99"; 
    savings = "0.000000"; 
    storeID = 6; 
    thumb = "http://www.gamersgate.com/img/boximgs/small/DD-CIV5CE.jpg"; 
    title = "Sid Meier's Civilization V: The Complete Edition"; 
}, 
{ 
    dealID = "KvMAVxjVD4GOORbG2LPd%2FYy6ZVpyAyZPj%2FCiS0nwOhQ%3D"; 
    dealRating = "0.0"; 
    gameID = 61; 
    internalName = SIDMEIERSCIVILIZATIONV; 
    lastChange = 1412962393; 
    metacriticLink = "/game/pc/sid-meiers-civilization-v"; 
    metacriticScore = 90; 
    normalPrice = "29.99"; 
    releaseDate = 1285027200; 
    salePrice = "29.99"; 
    savings = "0.000000"; 
    storeID = 6; 
    thumb = "http://cdn.akamai.steamstatic.com/steam/apps/8930/capsule_sm_120.jpg?t=1414605253"; 
    title = "Sid Meier's Civilization V"; 
} 
) 

但我該如何去訪問任何個人的價值?對於我來說,在字典中找到價值似乎沒有關鍵。我如何訪問各個元素?

回答

1

從響應1的結果是一個數組,所以下面的代碼應有助於

NSArray *results = [NSJSONSerialization JSONObjectWithData:response1 options:NSJSONReadingMutableContainers error:nil]; 
    for(NSDictionary* dict in results){ 
     for(NSString* key in dict.allKeys){ 
      NSLog(@"keys :%@ ,value: %@",key,[dict objectForKey:key]); 
     } 
    } 
0

您可以使用API​​ allKeys獲取一組鍵並在循環中逐一訪問。

NSArray *keys = [myDictionary allKeys]; 

在你的情況

for (int i = 0; i < [results count]; i++) { 
    NSArray *allkeys = [[results objectAtIndex:i] allKeys]; // you will get the keys list 
} 

希望這有助於。

+0

謝謝,我試圖 NSArray的*按鍵= [結果allKeys]; 但我有一個無法識別的選擇器發送到實例錯誤 – snowflakekiller 2014-11-21 07:52:14

+0

這是因爲響應是一個字典數組。您需要訪問每個數組元素,然後訪問字典。 – user2071152 2014-11-21 07:52:44

+0

@ user2071152 - 響應以數組開始,而不是字典 – 2014-11-21 07:55:08

0
NSArray *results = [NSJSONSerialization JSONObjectWithData:response1 options:NSJSONReadingMutableContainers error:nil]; 

for(NSDictionary* dict in results){ 
     [yourMutableArray addObject: dict]; 
    }