我有一点问题,我有一个json feed,我想在数组中显示那些Country = France的项目怎么办?谢谢你(Objective-C)
我的JSON:
{
"menu": "Fichier",
"commandes": [
{
"country": "France",
"city": "Paris"
},
{
"country": "USA",
"city": "New York"
},
{
"country": "Canada",
"city": "Quebec"
}
]
}
答案 0 :(得分:1)
首先使用NSJSONSerialization解析您的JSON数据
// data will be your Json NSData
NSError* jsonError;
NSDictionary* jsonDictn = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
NSMutableArray * aray = [[jsonDictn objectForKey:@"commandes"] mutableCopy];
for (NSDictionary* tmpdictn in aray) {
NSString * tmpstr = [tmpdictn objectForKey:@"country"];
if (![tmpstr isEqualToString:@"France"]) {
[aray removeObject:tmpdictn];
}
}
NSLog(@"aray: %@",aray);
答案 1 :(得分:0)
这会有所帮助:
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"commandes" ascending:TRUE];
[sourceArr sortUsingDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
答案 2 :(得分:0)
您需要使用NSPredicate
来搜索数组中的数据
使用下面的代码
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"country == %@", @"France"];
NSArray *filteredArray = [[arr objectForKey:@"commandes"] filteredArrayUsingPredicate:predicate];