gpt4 book ai didi

ios - 将字典数组拆分为子数组

转载 作者:行者123 更新时间:2023-12-01 17:48:01 25 4
gpt4 key购买 nike

以下是我的JSON响应的一部分,其中“results”是一个字典数组:

{
"results": [
{
"id": 6,
"genre_name": "Action",
"cover_image": "http://54.254.204.81/images/Action.png"
},
{
"id": 5,
"genre_name": "Adventure",
"cover_image": "http://54.254.204.81/images/Adventure.png"
},
{
"id": 4,
"genre_name": "Romance",
"cover_image": "http://54.254.204.81/images/Romance.png"
},
{
"id": 3,
"genre_name": "Sci-Fci",
"cover_image": "http://54.254.204.81/images/Sci-Fi.png"
},
{
"id": 1,
"genre_name": "Guide",
"cover_image": "http://54.254.204.81/images/Adventure_XHLbNfN.png"
},
{
"id": 2,
"genre_name": "Horror",
"cover_image": "http://54.254.204.81/images/Phineas-and-Ferb-Christmas-Wallpaper.jpg"
},
{
"id": 7,
"genre_name": "Emotional",
"cover_image": "http://54.254.204.81/images/a0fea991287cf41b6b9c4aa16196517f.jpg"
},
{
"id": 8,
"genre_name": "abcd",
"cover_image": "http://54.254.204.81/images/logo_text_S0KyzUW.png"
}
]
}

现在,我有另一个JSON响应,其中“ genres”是一个数组,其中包含对象,这些对象是“ results”键对象为“id”的数组对象的子集。
{
"genres": [
3,
1
]
}

现在,我可以将“ results”分成两个数组“ results1”和“ results2”,例如:
{
"results1": [
{
"id": 6,
"genre_name": "Action",
"cover_image": "http://54.254.204.81/images/Action.png"
},
{
"id": 5,
"genre_name": "Adventure",
"cover_image": "http://54.254.204.81/images/Adventure.png"
},
{
"id": 4,
"genre_name": "Romance",
"cover_image": "http://54.254.204.81/images/Romance.png"
},
{
"id": 2,
"genre_name": "Horror",
"cover_image": "http://54.254.204.81/images/Phineas-and-Ferb-Christmas-Wallpaper.jpg"
},
{
"id": 7,
"genre_name": "Emotional",
"cover_image": "http://54.254.204.81/images/a0fea991287cf41b6b9c4aa16196517f.jpg"
},
{
"id": 8,
"genre_name": "abcd",
"cover_image": "http://54.254.204.81/images/logo_text_S0KyzUW.png"
}
]
}


{
"results2": [
{
"id": 3,
"genre_name": "Sci-Fci",
"cover_image": "http://54.254.204.81/images/Sci-Fi.png"
},
{
"id": 1,
"genre_name": "Guide",
"cover_image": "http://54.254.204.81/images/Adventure_XHLbNfN.png"
}
]
}

最佳答案

为此,您需要像这样将NSPredicateINNOT一起使用。

首先从您的两个JSON响应中创建resultArraygenresArray

NSArray *resultArray = [firstJSONResponse objectForKey:@"results"];
NSArray *genresArray = [secondJSONResponse objectForKey:@"genres"];

现在,使用 resultArray过滤 NSPredicate以获得结果。

对于Result1
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (id IN %@)",genresArray];
NSArray *firstSplitArray = [resultArray filteredArrayUsingPredicate:predicate];
NSLog(@"%@",firstSplitArray);

对于Result2
predicate = [NSPredicate predicateWithFormat:@"id IN %@",genresArray];
NSArray *secondSplitArray = [resultArray filteredArrayUsingPredicate:predicate];
NSLog(@"%@",secondSplitArray);

关于ios - 将字典数组拆分为子数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41461363/

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