在ios中调用嵌套的json对象

时间:2015-07-16 06:51:58

标签: ios json afnetworking

我正在开发一个ios项目。我用AFNetworking来调用json。 我只想调用一个特定的json对象。这意味着一个特定的嵌套json对象。请帮助我这样做。作为一个例子,这是我的json feed。在这里,我想只在缩略图中调用第一个URL。

    {     status:"ok",
      count:50,
      count_total:44444,
      pates:333,
    - posts:[
        - {
            id:3333,
            type:"post",
            - thumbnail_images :{
                - full : {
                    url:"",
                    width:666
                },
                -thumbnail:{
                    url:"",
                    width:333

                    },
                - large:{
                    url:"",
                    width:777
                }
            }
        },
        - {
            id:3334,
            type:"post",
            - thumbnail_images :{
                - full : {
                    url:"",
                    width:6644
                },
                -thumbnail:{
                    url:"",
                    width:3345

                    },
                - large:{
                    url:"",
                    width:7778
                }
            }
        },
        - {
            id:333344,
            type:"post",
            - thumbnail_images :{
                - full : {
                    url:"",
                    width:6665
                },
                -thumbnail:{
                    url:"",
                    width:3336

                    },
                - large:{
                    url:"",
                    width:7770
                }
            }
        },

    ]

}

这也是我的客观C方法。

- (void)loadImagesToCategoryone
{
    imagespost = nil;
    NSString *urlOne = [NSString stringWithFormat:@"some url"];
    AFHTTPRequestOperationManager *managerone = [AFHTTPRequestOperationManager manager];
    [managerone GET:urlOne parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

        imagesposts = (NSDictionary *)responseObject;
        NSArray *resultone = [imagesposts objectForKey:@"posts"];
        imagespost = [NSMutableArray array];
        for (NSDictionary *imageone in resultone)
        {

                Categories *categoryone = [Categories new];
            NSArray *firstArray = [imageone objectForKey:@"thumbnail_images"];
            NSLog(@"%@",firstArray[0]);
        }

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    }];
}

1 个答案:

答案 0 :(得分:2)

请注意thumbnail_images是字典而不是数组。

NSArray *resultone = [imagesposts objectForKey:@"posts"];
if ([resultone count]) {
    //If need thumbnail of first post only then
    NSDictionary *firstPost = resultone[0];
    Categories *categoryone = [Categories new];
    NSDictionary *imageDetails = [imageone objectForKey:@"thumbnail_images"];
    NSDictionary *thumbnailImage = [imageDetails objectForKey:@"thumbnail"];
    NSString *urlString = [thumbnailImage objectForKey:@"url"];
        //Assume thumbnail image is required
        NSLog(@"%@",urlString);
}