在 ios 中调用嵌套 json 个对象

call for nested json objects, in ios

我正在做一个 ios 项目。我使用 AFNetworking 调用 json。 我只想调用一个特定的 json 对象。这意味着对于特定的嵌套 json object.please 帮助我做到这一点。例如,这是我的 json 供稿。在这里我只想调用缩略图内的第一个 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
                }
            }
        },

    ]

}

这也是我的objective-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) {

    }];
}

请注意 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);
}