我试着根据状态显示数据

时间:2016-03-07 08:14:22

标签: ios objective-c json logic

我有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;
    },
 }

2 个答案:

答案 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];
}