JSON如何在不同的列表中调用对象

时间:2012-10-21 21:38:47

标签: iphone objective-c ios json

我目前有一个JSON API,如下所示:

{
  "posts": [
    {
      "id": 277,
      "title": "test title",
      "content": "test content",
      "attachments": [
        {
          "url": "http:\/\/www.example.com\/bar-icon-help@2x.png"
        }
    }
}    

我正在尝试使用“附件”下的“网址”。请看看我做错了什么:

-(void)dataRequestCompletedWithJsonObject.(id)jsonObject
{
    NSDictionary *recipeDictionary = (NSDictionary*)jsonObject;

    NSArray* recipeArray = (NSArray*)[recipeDictionary objectForKey:@"posts"];

    self.recipes = [[NSMutableArray alloc] init];

    for (NSDictionary* dic in recipeArray) {

        Recipe *recipe = [[Recipe alloc] init];

        recipe.name = [dic objectForKey:@"title"];
        recipe.thumbNail = [[dic objectForKey:@"attachements"]objectForKey:@"url"];
        recipe.twitterShareCount = [[dic objectForKey:@"id"] intValue];

        [recipes addObject:recipe];
    }

主要是试图找出我应该使用的而不是这一行:

recipe.thumbNail = [[dic objectForKey:@"attachements"]objectForKey:@"url"];

感谢任何帮助过的人!

1 个答案:

答案 0 :(得分:1)

附件对象中有一系列dicts。

[[[dic objectForKey:@"attachements"] objectAtIndex:0] objectForKey:@"url"]