我有一个从JSON解析的对象数组。我的JSON看起来像这样;
- message: [
-{
movie_id: 30,
movie_name: "Scream",
category_name: "Horror"
},
-{
movie_id: 31,
movie_name: "Avengers",
category_name: "Action"
},
-{
movie_id: 32,
movie_name: "Friday the 13th",
category_name: "Horror"
},
-{
movie_id: 33,
movie_name: "The ring",
category_name: "Horror"
},
-{
movie_id: 34,
movie_name: "Die Hard",
category_name: "Action"
},
-{
movie_id: 35,
movie_name: "The ring 2",
category_name: "Horror"
},
....
它是一部包含许多电影的JSON电影数组。我无法做的是解析每个对象并按类别中的category_name排列它们,所以所有的"恐怖"将被组合在一起,所有动作,以及所有浪漫等等。但是......我需要能够在每个类别的开头放置一个String值。所以数组可能如下所示:
Index
0 "Horror"
1 Movie Object
2 Movie Object
3 Movie Object
4 "Romance"
5 Movie Object
6 Movie Object
7 "Action"
8 Movie Object
9 Movie Object
10 Movie Object
我有以下参数的电影对象;
NSString *movie_id
NSString * movie_name
NSString *category_name
非常感谢任何帮助。
答案 0 :(得分:2)
我建议对解析后的数据采用略有不同的结构。
为什么不创建数组的字典(使用类别名称作为键),而不是混合字符串和对象的数组?只需遍历原始的JSON即可。对于每个电影对象,请检查该类别名称是否存在该键。如果没有,请添加一个新数组作为该类别名称的值。然后将影片对象添加到数组中。
你的最后一本字典会是这样的:
@{ @"Horror" : @[obj1, obj2, obj 3],
@"Romance" : @[obj4, obj5, obj 6],
@"Action" : @[obj7, obj8, obj 9] }
通过这种方式,您可以遍历每个类别,并且还可以遍历电影中的每个类别
答案 1 :(得分:1)
假设您有阵列消息,那么您可以将它们排列为
NSArray *categories = [message valueForKeyPath:@"@distinctUnionOfObjects.category_name"];
NSMutableArray *results = [NSMutableArray new];
for (NSString *categoryName in categories)
{
NSArray *groupArray = [message filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"category_name = %@", categoryName]];
[results addObject:categoryName];
[results addObjectsFromArray:groupArray];
}
和你的最终数组我的意思是结果
(
Horror,
{
"category_name" = Horror;
"movie_id" = 30;
"movie_name" = Scream;
},
{
" movie_name" = "Friday the 13th";
"category_name" = Horror;
"movie_id" = 32;
},
{
"category_name" = Horror;
"movie_id" = 33;
"movie_name" = "The ring";
},
{
"category_name" = Horror;
"movie_id" = 35;
"movie_name" = "The ring 2";
},
Action,
{
"category_name" = Action;
"movie_id" = 31;
"movie_name" = Avengers;
},
{
"category_name" = Action;
"movie_id" = 34;
"movie_name" = "Die Hard";
}
)
希望这是你想要的。 但这不是一个好的编码实践,在同一个数组中有不同的数据类型(字符串和字典)。所以你应该像这样解析它们
NSArray *categories = [message valueForKeyPath:@"@distinctUnionOfObjects.category_name"];
NSMutableDictionary *results = [NSMutableDictionary new];
for (NSString *categoryName in categories)
{
NSArray *groupArray = [message filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"category_name = %@", categoryName]];
[results setObject:groupArray forKey:categoryName];
}
你的结果将是。
{
Action = (
{
"category_name" = Action;
"movie_id" = 31;
"movie_name" = Avengers;
},
{
"category_name" = Action;
"movie_id" = 34;
"movie_name" = "Die Hard";
}
);
Horror = (
{
"category_name" = Horror;
"movie_id" = 30;
"movie_name" = Scream;
},
{
" movie_name" = "Friday the 13th";
"category_name" = Horror;
"movie_id" = 32;
},
{
"category_name" = Horror;
"movie_id" = 33;
"movie_name" = "The ring";
},
{
"category_name" = Horror;
"movie_id" = 35;
"movie_name" = "The ring 2";
}
);
}
现在选择你想要的任何东西。