OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在ios objective-c 中过滤Json数据 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:23
标题: ios - 如何在ios objective-c 中过滤Json数据

JSON 解析有问题。
我得到 JSON 数据,我想显示与 restid 匹配的项目。
我有一个 tableView 来显示不同的餐厅信息,每个餐厅也有自己的评论单元来评论这家餐厅。
所以,我会过滤 JSON 数据。
让每个餐厅评论单元都能获得自己的评论。

这是我的 JSON:

(
    {
    comment = "very good";
    food = chicken;
    name = Tom;
    restid = 1;
    score = 4;
},
    {
    comment = nice;
    food = coffee;
    name = Jack;
    restid = 3;
    score = 3;
},
    {
    comment = tasty;
    food = pizza;
    name = Mary;
    restid = 17;
    score = 5;
},
    {
    comment = unlike;
    food = none;
    name = Gigi;
    restid = 33;
    score = 1;
},
    {
    comment = delicious;
    food = juice;
    name = Bruce;
    restid = 45;
    score = 5;
}
)

这是我的代码:

它可以显示我的 tableView 中的所有数据以及不同餐厅评论单元中的所有相同数据。

- (void)getCommentInfoid)sender {

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSURL *url = [NSURL URLWithString"http://localhost:8888/foodComment_json.php"];

        NSData *data = [NSData dataWithContentsOfURL:url];
        NSArray *comment = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSLog(@"Comment is %@",comment);

        self.commentArray = [NSMutableArray arrayWithArray:comment];

        dispatch_async(dispatch_get_main_queue(), ^{
            [self.detailCommentTableView reloadData];
        });
    });
}



Best Answer-推荐答案


试试这个,我没有测试代码,如果你有问题,请告诉我。

NSArray *filteredArray = [comment filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id elem, NSDictionary *bindings) {
    return ([elem objectForKey"restid"] == <your_rest_id>)
}]];

关于ios - 如何在ios objective-c 中过滤Json数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39201471/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4