-1

我正在做一个 Json 解析,因为我需要解析“TStatus”键来显示表格。

在该 TStatus 键中,值为 3 种类型。它们是 100001、100002、100003。

我只想要 100001 个值...如何获取这些值并将其存储在数组中。

NSMutableArray *array = [arrayOfoccupiedTables valueForKey:@"TStatus"];

回应是

 {
        Msg = "";
        TStatus = 100003;
        "Table_Id" = 446;
    },
        {
        Msg = "";
        TStatus = 100001;
        "Table_Id" = 447;
    },
        {
        Msg = "";
        TStatus = 100001;
        "Table_Id" = 448;
    },
        {
        Msg = "";
        TStatus = 100001;
        "Table_Id" = 449;
    },
        {
        Msg = "";
        TStatus = 100001;
        "Table_Id" = 450;
    },
        {
        Msg = "";
        TStatus = 100001;
        "Table_Id" = 451;
    },
        {
        Msg = "";
        TStatus = 100001;
        "Table_Id" = 452;
    },
        {
        Msg = "";
        TStatus = 100003;
        "Table_Id" = 453;
    },
        {
        Msg = "";
        TStatus = 100001;
        "Table_Id" = 454;
    },
        {
        Msg = "";
        TStatus = 100001;
        "Table_Id" = 455;
    },
        {
        Msg = "";
        TStatus = 100001;
        "Table_Id" = 456;
    },
        {
        Msg = "";
        TStatus = 100001;
        "Table_Id" = 457;
    },
        {
        Msg = "";
        TStatus = 100001;
        "Table_Id" = 458;
    },
        {
        Msg = "";
        TStatus = 100001;
        "Table_Id" = 459;
    },
        {
        Msg = "";
        TStatus = 100001;
        "Table_Id" = 460;
    },
        {
        Msg = "";
        TStatus = 100001;
        "Table_Id" = 461;
    },
        {
        Msg = "";
        TStatus = 100001;
        "Table_Id" = 462;
    },
        {
        Msg = "";
        TStatus = 100003;
        "Table_Id" = 463;
    },
        {
        Msg = "";
        TStatus = 100001;
        "Table_Id" = 464;
    },
        {
        Msg = "";
        TStatus = 100001;
        "Table_Id" = 465;
    },
        {
        Msg = "";
        TStatus = 100001;
        "Table_Id" = 466;
    },
        {
        Msg = "";
        TStatus = 100001;
        "Table_Id" = 467;
    },
        {
        Msg = "";
        TStatus = 100001;
        "Table_Id" = 468;
    },
        {
        Msg = "";
        TStatus = 100001;
        "Table_Id" = 469;
    },
        {
        Msg = "";
        TStatus = 100001;
        "Table_Id" = 470;
    },
        {
        Msg = "";
        TStatus = 100001;
        "Table_Id" = 471;
    },
        {
        Msg = "";
        TStatus = 100001;
        "Table_Id" = 472;
    },
        {
        Msg = "";
        TStatus = 100001;
        "Table_Id" = 473;
    },
        {
        Msg = "";
        TStatus = 100001;
        "Table_Id" = 474;
    },
        {
        Msg = "";
        TStatus = 100001;
        "Table_Id" = 477;
    },
        {
        Msg = "";
        TStatus = 100001;
        "Table_Id" = 478;
    },
        {
        Msg = "";
        TStatus = 100001;
        "Table_Id" = 479;
    }
)

请帮我摆脱这些家伙...在此先感谢

4

4 回答 4

2

您必须使用NSPredicate 过滤数组。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"TStatus == 100001"];

NSArray *filtered = ["Your Array" filteredArrayUsingPredicate:predicate];

在过滤后的数组中,您将获得Tstatus = 100001 的值;

于 2012-11-06T09:06:11.073 回答
1

您有一个包含所有数据的数组,例如,arrList当您想要获取 TStatus 为 100001 的整个数据时,只需使用下面的代码,然后使用下面的代码过滤即可。

NSMutableArray *arrTStatus = [[NSMutableArray alloc]init];
 for (int i =0; i<[arrList count]; i++) 
 {
     if ([[[arrList objectAtIndex:i]valueForKey:@"TStatus"] isEqualToString:@"100001"] ) 
     {
          [arrTStatus addObject:[arrList objectAtIndex:i]];   
     }
 }
于 2012-11-06T09:11:50.083 回答
0

尝试这个

for (NSDictionary *status in statuses) {

NSString *Tstatus = [status objectForKey:@"TStatus"]
if([Tstatus isEqualToString:@"100001"){
//Do your code here
}
}

参考这个关于json的更多信息

于 2012-11-06T09:21:13.233 回答
0

希望这会有所帮助

[allObject filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"TStatus = 100001"]];
于 2012-11-06T09:15:43.080 回答