function findId(data, idToLookFor) {
var categoryArray = data.category;
for (var i = 0; i < categoryArray.length; i++) {
if (categoryArray[i].id == idToLookFor) {
return(categoryArray[i].product);
}
}
}
var item = findId(data, 1);
答案 0 :(得分:0)
为什么不使用库,例如:http://www.jsonmodel.com/
无论如何,这里是ObjectiveC中代码的版本:
+(id)findId:(NSMutableArray *)data idToLookFor:(NSNumber *)idToLookFor
{
NSMutableArray *categoryArray = [data valueForKey:@"category"];
for (int i = 0; i < categoryArray.count; i++) {
if ([[categoryArray[i] valueForKey:@"id"] isEqualToValue:idToLookFor]) {
return [categoryArray[i] valueForKey:@"product"];
}
}
return nil;
}