gpt4 book ai didi

ios - 在 objective-c 中解析JSON信息(多个级别)

转载 作者:行者123 更新时间:2023-12-01 18:20:10 28 4
gpt4 key购买 nike

我可以解析JSON文件的一部分,但是如果我想更深入地了解结构,就会迷路了。这是我的JSON:

{
"album":[
{
"album_titre":"Publicité",
"album_photo":"blabla.jpg",
"album_videos":[
{
"titre_video":"Chauffage Compris",
"duree_video":"01'25''",
"photo_video":"chauffage.jpg",
"lien_video":"www.bkjas.jhas.kajs"
},
{
"titre_video":"NIFFF 2012",
"duree_video":"01'43''",
"photo_video":"nifff.jpg",
"lien_video":"www.bkjas.jhas.kajs"
}
]
},
{
"album_titre":"Events",
"album_photo":"bloublou.jpg",
"album_videos":[
{
"titre_video":"Auvernier Jazz",
"duree_video":"01'15''",
"photo_video":"auvernier.jpg",
"lien_video":"www.bkjas.jhas.kajs"
},
{
"titre_video":"NIFFF 2011",
"duree_video":"01'03''",
"photo_video":"nifff2011.jpg",
"lien_video":"www.bkjas.jhas.kajs"
}
]
},
{
"album_titre":"Culture",
"album_photo":"bilibl.jpg"
},
{
"album_titre":"Postproduction",
"album_photo":"bizoubzou"
}
]
}

这是我的Objective-C代码:
- (void)fetchedData:(NSData *)responseData {

NSError* error;
NSDictionary *document = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

if (document==nil)
{
NSLog( @"oops\n%@", error);
}

NSArray *album = document[@"album"];


for( NSDictionary *albumDictionary in album )
{
[album_titre addObject:albumDictionary[@"album_titre"]];
[album_photo addObject:albumDictionary[@"album_photo"]];

for( NSDictionary *album_videosDictionary in albumDictionary[@"album_videos"])
{
[titre_video addObject:album_videosDictionary[@"titre_video"]];
[duree_video addObject:album_videosDictionary[@"duree_video"]];
[photo_video addObject:album_videosDictionary[@"photo_video"]];
[lien_video addObject:album_videosDictionary[@"lien_video"]];
}

}

[self.tableView reloadData];

}

我无法实现的是创建一个数组,例如包含与“album_titre”:“publicité”相对应的所有“titre_video”。因此它应该包含“Chauffage Compris”和“Nifff 2012”。

我知道这是一个简单的问题,但我已经搜索了一段时间,但仍然无法做到。

非常感谢。

尼古拉斯

最佳答案

关键是要了解JSON结构:

{}--这表示基础对象是字典。

[ ]  -- this means the underlyingobject is an array

- (void)fetchedData:(NSData *)responseData {

NSError* error;
NSDictionary *document = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

// all titre:video for album_titre:publicite
NSArray *albumArray = [document objectForKey:@"album"];
NSDictionary *dict = [albumArray objectAtindex:0];
NSArray *videos = [dict objectForKey:@"album_videos"];

// to fetch Videos inside album_videos
// here you will get al the videos inside key titre_video
NSMutableArray *titreVideoArray = [[NSMutableArray alloc]init];
for(int i=0; i< videos.count; i++){
NSDictionary *dict = [videos objectAtindex:i];
NSArray *titreVideos = [dict objectForKey:@"titre_video"];
[titreVideoArray addObject: titreVideos];
}
}

似乎您想获取与不同“album_titre”相关的所有视频。
我建议您使用NSPredicate。
NSArray *videos = [self videosArrayForTitle:@"Publicité" albumArray:albumArray];

在这里,我们从上方传递title和albumArray以获取视频数组。
- (NSArray *)videosArrayForTitle:(NSString *)title albumArray:(NSArray *)albumArray{
NSPredicate *resultPredicate=[NSPredicate predicateWithFormat:@"SUBQUERY(album_titre, $content, $content CONTAINS %@).@count > 0", title];
NSArray *searchResults=[albumArray filteredArrayUsingPredicate:resultPredicate];
NSArray *videos = [searchResults objectForKey:@"album_videos"];
return videos;
}

关于ios - 在 objective-c 中解析JSON信息(多个级别),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17924480/

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