how to Filter the Json data in ios objective c -
i have problem json parsing.
json data , want display items matched restid.
have tableview display different restaurant information , every restaurant have own comment cell comment restaurant.
so,i filter json data.
let each restaurant comment cell can own comment.
this 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; } )
this code:
can display data in tableview , same data in different restaurant comment cell.
- (void)getcommentinfo:(id)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 %@",comment); self.commentarray = [nsmutablearray arraywitharray:comment]; dispatch_async(dispatch_get_main_queue(), ^{ [self.detailcommenttableview reloaddata]; }); }); }
try this, didn't test code let me know if have problems.
nsarray *filteredarray = [comment filteredarrayusingpredicate:[nspredicate predicatewithblock:^bool(id elem, nsdictionary *bindings) { return ([elem objectforkey:@"restid"] == <your_rest_id>) }]];
Comments
Post a Comment