我有json响应,我只需要显示状态= 1的数据,那么我该怎么做才能帮助我
我的回答是
JSON: (
{
name = "Kate Bell";
phone = "(555) 564-8583";
status = 0;
},
{
name = "Daniel Higgins";
phone = "(408) 555-5270";
status = 0;
},
{
name = "John Appleseed";
phone = "888-555-5512";
status = 0;
},
{
name = "Anna Haro";
phone = "<null>";
status = 1;
},
{
name = "Hank Zakroff";
phone = "<null>";
status = 1;
},
}
答案 0 :(得分:1)
使用谓词来获取状态为1的过滤数组
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status = %d", 1];
NSArray *arrFilter = [YourWholeArray filteredArrayUsingPredicate:predicate];
NSLog(@"array with status 1 ->%@",arrFilter);
答案 1 :(得分:1)
根据该假设,您的JSON响应以数组
开头如果你真的需要JSON的字符串,可以通过查看operation.responseString
来获取它。
我从here
得到答案 responseObject
是NSArray或NSDictionary。您可以使用isKindOfClass:
在运行时检查:
if ([responseObject isKindOfClass:[NSArray class]]) {
NSArray *responseArray = responseObject;
/* do something with responseArray */
NSPredicate *predi = [NSPredicate predicateWithFormat:@"status = 1"];
NSArray *newarray = [responseArray filteredArrayUsingPredicate:predi];
} else if ([responseObject isKindOfClass:[NSDictionary class]]) {
NSDictionary *responseDict = responseObject;
/* do something with responseDict */
NSArray *responseArray = responseDict[@"yourrootKeyname"];
NSPredicate *predi = [NSPredicate predicateWithFormat:@"status = 1"];
NSArray *newarray = [responseArray filteredArrayUsingPredicate:predi];
}