2014-11-21 36 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]; 
    }