gpt4 book ai didi

ios - 在 ios 中调用嵌套的 json 对象

转载 作者:行者123 更新时间:2023-11-29 10:27:24 26 4
gpt4 key购买 nike

我正在做一个 ios 项目。我使用 AFNetworking 来调用 json。我只想调用一个特定的 json 对象。这意味着特定的嵌套 json 对象。请帮助我做到这一点。例如,这是我的 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);
}

关于ios - 在 ios 中调用嵌套的 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31447184/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com