如何从 JSON 树中检索特定的 ID
How to retrieve specific id from JSON tree
我可以在 table 视图中打印 sub_category
和 products
,它工作正常,但我无法打印 id 的特定详细信息,例如我如何才能打印 products_id
=213
的详细信息以获得 name
,image
,model
?
我的 table 视图看起来像这样 [categories>name] 然后 [sub_category>name] 然后我需要选择的 products_id name
,image
,model
我正在使用 segue
我的JSON:
{"categories":[
{"category_id":"100","name":"Shop By Room","sub_category":[
{"category_id":"72","name":"BEDROOM","sub_category":0,"product_total":"11","products":[
{"product_id":"138","name":"Jewellery Holder","description":"","meta_description":"","meta_keyword":"","tag":"","model":"28223","sku":"1050426","upc":"","ean":"","jan":"","isbn":"","mpn":"","location":"","quantity":"6","stock_status":"In Stock","image":"data\/bedroom\/28223-clear.jpg","manufacturer_id":null,"manufacturer":null,"price":"0.0000","special":null,"reward":"0","points":"0","tax_class_id":"0","weight_class_id":"1","length":"0.00000000","width":"0.00000000","height":"0.00000000","length_class_id":"1","subtract":"1","rating":0,"reviews":0,"minimum":"1","sort_order":"1","status":"1","viewed":"18"},
{"product_id":"139","name":"Jewellery Holder","description":"","meta_description":"","meta_keyword":"","tag":"","model":"28223","sku":"1050507","upc":"","ean":"","jan":"","isbn":"","mpn":"","location":"","quantity":"6","stock_status":"In Stock","image":"data\/bedroom\/28223-green.jpg","manufacturer_id":null,"manufacturer":null,"price":"0.0000","special":null,"reward":"0","points":"0","tax_class_id":"0","weight_class_id":"1","length":"0.00000000","width":"0.00000000","height":"0.00000000","length_class_id":"1","subtract":"1","rating":0,"reviews":0,"minimum":"1","sort_order":"1","status":"1","viewed":"6"},
{"product_id":"136","name":"Mirror","description":"","meta_description":"","meta_keyword":"","tag":"","model":"28225","sku":"1050509","upc":"","ean":"","jan":"","isbn":"","mpn":"","location":"","quantity":"6","stock_status":"Out Of Stock","image":"data\/bedroom\/28225-green.jpg","manufacturer_id":null,"manufacturer":null,"price":"0.0000","special":null,"reward":"0","points":"0","tax_class_id":"0","weight_class_id":"1","length":"0.00000000","width":"0.00000000","height":"0.00000000","length_class_id":"1","subtract":"1","rating":0,"reviews":0,"minimum":"1","sort_order":"1","status":"1","viewed":"6"},
{"product_id":"137","name":"Mirror","description":"","meta_description":"","meta_keyword":"","tag":"","model":"28225","sku":"1050430","upc":"","ean":"","jan":"","isbn":"","mpn":"","location":"","quantity":"6","stock_status":"In Stock","image":"data\/bedroom\/28225-clear.jpg","manufacturer_id":null,"manufacturer":null,"price":"0.0000","special":null,"reward":"0","points":"0","tax_class_id":"0","weight_class_id":"1","length":"0.00000000","width":"0.00000000","height":"0.00000000","length_class_id":"1","subtract":"1","rating":0,"reviews":0,"minimum":"1","sort_order":"1","status":"1","viewed":"6"},
{"product_id":"213","name":"Night Light","description":"","meta_description":"","meta_keyword":"","tag":"","model":"15478","sku":"1064373","upc":"","ean":"","jan":"","isbn":"","mpn":"","location":"","quantity":"1","stock_status":"In Stock","image":"data\/timeless-lighting\/15478-1050517-clear.png","manufacturer_id":null,"manufacturer":null,"price":"0.0000","special":null,"reward":"0","points":"0","tax_class_id":"0","weight_class_id":"1","length":"0.00000000","width":"0.00000000","height":"0.00000000","length_class_id":"1","subtract":"1","rating":0,"reviews":0,"minimum":"1","sort_order":"1","status":"1","viewed":"4"}]},
{"category_id":"67","name":"DINING ROOM","sub_category":0,"product_total":"74","products":[
{"product_id":"248","name":"Amuse Bouche","description":"","meta_description":"","meta_keyword":"","tag":"","model":"28106","sku":"1056479","upc":"","ean":"","jan":"","isbn":"","mpn":"","location":"","quantity":"6","stock_status":"In Stock","image":"data\/dining\/28106-ambience.jpg","manufacturer_id":null,"manufacturer":null,"price":"0.0000","special":null,"reward":"0","points":"0","tax_class_id":"0","weight_class_id":"1","length":"0.00000000","width":"0.00000000","height":"0.00000000","length_class_id":"1","subtract":"1","rating":0,"reviews":0,"minimum":"1","sort_order":"1","status":"1","viewed":"17"},
{"product_id":"239","name":"Amuse Bouche","description":"","meta_description":"","meta_keyword":"","tag":"","model":"25369","sku":"1050857","upc":"","ean":"","jan":"","isbn":"","mpn":"","location":"","quantity":"6","stock_status":"In Stock","image":"data\/dining\/25369-1050857-clear.png","manufacturer_id":null,"manufacturer":null,"price":"0.0000","special":null,"reward":"0","points":"0","tax_class_id":"0","weight_class_id":"1","length":"0.00000000","width":"0.00000000","height":"0.00000000","length_class_id":"1","subtract":"1","rating":0,"reviews":0,"minimum":"1","sort_order":"1","status":"1","viewed":"13"}
。
.
.
我的代码:
-(void)run{
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
for (NSDictionary *dict in [appDelegate.dic objectForKey:@"categories"]) {
if (![dict[@"name"] isEqualToString:_subModel]) continue;
for (NSDictionary *subcategory in [dict valueForKey:@"sub_category"])
{
if (![subcategory[@"name"] isEqualToString:_item]) continue;
for (NSDictionary *product in subcategory[@"products"]) {
//Update
itemDetail *item = [[itemDetail alloc]init];
item.strId = product[@"product_id"];
item.strName = product[@"name"];
item.strImage = product[@"image"];
[_arrayItem addObject:product];
NSLog(@"ITEM: %@", _arrayItem);
//NSLog(@" %@, %@, %@", item.strId, item.strName, item.strImage);
[_arraySubCategory addObject:product[@"name"]];
}
}
}
}
创建一个产品对象,将名称、图像等作为其属性。然后将此对象添加到您的数组中。
.
.
.
for (NSDictionary *product in subcategory[@"products"]) {
Product *product = [[Product alloc]init];
product.name = product[@"name"];
product.image = product[@"image"]; //and so on
[_arraySubCategory addObject:product];
}
现在,当您想从数组中获取数据时,
for (Product *product in _arraySubCategory)
{
if([product.name isEqualToString:@"abc"])
{
NSLog(@"%@",product.name);
//do what you want.
}
}
我可以在 table 视图中打印 sub_category
和 products
,它工作正常,但我无法打印 id 的特定详细信息,例如我如何才能打印 products_id
=213
的详细信息以获得 name
,image
,model
?
我的 table 视图看起来像这样 [categories>name] 然后 [sub_category>name] 然后我需要选择的 products_id name
,image
,model
我正在使用 segue
我的JSON:
{"categories":[
{"category_id":"100","name":"Shop By Room","sub_category":[
{"category_id":"72","name":"BEDROOM","sub_category":0,"product_total":"11","products":[
{"product_id":"138","name":"Jewellery Holder","description":"","meta_description":"","meta_keyword":"","tag":"","model":"28223","sku":"1050426","upc":"","ean":"","jan":"","isbn":"","mpn":"","location":"","quantity":"6","stock_status":"In Stock","image":"data\/bedroom\/28223-clear.jpg","manufacturer_id":null,"manufacturer":null,"price":"0.0000","special":null,"reward":"0","points":"0","tax_class_id":"0","weight_class_id":"1","length":"0.00000000","width":"0.00000000","height":"0.00000000","length_class_id":"1","subtract":"1","rating":0,"reviews":0,"minimum":"1","sort_order":"1","status":"1","viewed":"18"},
{"product_id":"139","name":"Jewellery Holder","description":"","meta_description":"","meta_keyword":"","tag":"","model":"28223","sku":"1050507","upc":"","ean":"","jan":"","isbn":"","mpn":"","location":"","quantity":"6","stock_status":"In Stock","image":"data\/bedroom\/28223-green.jpg","manufacturer_id":null,"manufacturer":null,"price":"0.0000","special":null,"reward":"0","points":"0","tax_class_id":"0","weight_class_id":"1","length":"0.00000000","width":"0.00000000","height":"0.00000000","length_class_id":"1","subtract":"1","rating":0,"reviews":0,"minimum":"1","sort_order":"1","status":"1","viewed":"6"},
{"product_id":"136","name":"Mirror","description":"","meta_description":"","meta_keyword":"","tag":"","model":"28225","sku":"1050509","upc":"","ean":"","jan":"","isbn":"","mpn":"","location":"","quantity":"6","stock_status":"Out Of Stock","image":"data\/bedroom\/28225-green.jpg","manufacturer_id":null,"manufacturer":null,"price":"0.0000","special":null,"reward":"0","points":"0","tax_class_id":"0","weight_class_id":"1","length":"0.00000000","width":"0.00000000","height":"0.00000000","length_class_id":"1","subtract":"1","rating":0,"reviews":0,"minimum":"1","sort_order":"1","status":"1","viewed":"6"},
{"product_id":"137","name":"Mirror","description":"","meta_description":"","meta_keyword":"","tag":"","model":"28225","sku":"1050430","upc":"","ean":"","jan":"","isbn":"","mpn":"","location":"","quantity":"6","stock_status":"In Stock","image":"data\/bedroom\/28225-clear.jpg","manufacturer_id":null,"manufacturer":null,"price":"0.0000","special":null,"reward":"0","points":"0","tax_class_id":"0","weight_class_id":"1","length":"0.00000000","width":"0.00000000","height":"0.00000000","length_class_id":"1","subtract":"1","rating":0,"reviews":0,"minimum":"1","sort_order":"1","status":"1","viewed":"6"},
{"product_id":"213","name":"Night Light","description":"","meta_description":"","meta_keyword":"","tag":"","model":"15478","sku":"1064373","upc":"","ean":"","jan":"","isbn":"","mpn":"","location":"","quantity":"1","stock_status":"In Stock","image":"data\/timeless-lighting\/15478-1050517-clear.png","manufacturer_id":null,"manufacturer":null,"price":"0.0000","special":null,"reward":"0","points":"0","tax_class_id":"0","weight_class_id":"1","length":"0.00000000","width":"0.00000000","height":"0.00000000","length_class_id":"1","subtract":"1","rating":0,"reviews":0,"minimum":"1","sort_order":"1","status":"1","viewed":"4"}]},
{"category_id":"67","name":"DINING ROOM","sub_category":0,"product_total":"74","products":[
{"product_id":"248","name":"Amuse Bouche","description":"","meta_description":"","meta_keyword":"","tag":"","model":"28106","sku":"1056479","upc":"","ean":"","jan":"","isbn":"","mpn":"","location":"","quantity":"6","stock_status":"In Stock","image":"data\/dining\/28106-ambience.jpg","manufacturer_id":null,"manufacturer":null,"price":"0.0000","special":null,"reward":"0","points":"0","tax_class_id":"0","weight_class_id":"1","length":"0.00000000","width":"0.00000000","height":"0.00000000","length_class_id":"1","subtract":"1","rating":0,"reviews":0,"minimum":"1","sort_order":"1","status":"1","viewed":"17"},
{"product_id":"239","name":"Amuse Bouche","description":"","meta_description":"","meta_keyword":"","tag":"","model":"25369","sku":"1050857","upc":"","ean":"","jan":"","isbn":"","mpn":"","location":"","quantity":"6","stock_status":"In Stock","image":"data\/dining\/25369-1050857-clear.png","manufacturer_id":null,"manufacturer":null,"price":"0.0000","special":null,"reward":"0","points":"0","tax_class_id":"0","weight_class_id":"1","length":"0.00000000","width":"0.00000000","height":"0.00000000","length_class_id":"1","subtract":"1","rating":0,"reviews":0,"minimum":"1","sort_order":"1","status":"1","viewed":"13"}
。 . .
我的代码:
-(void)run{
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
for (NSDictionary *dict in [appDelegate.dic objectForKey:@"categories"]) {
if (![dict[@"name"] isEqualToString:_subModel]) continue;
for (NSDictionary *subcategory in [dict valueForKey:@"sub_category"])
{
if (![subcategory[@"name"] isEqualToString:_item]) continue;
for (NSDictionary *product in subcategory[@"products"]) {
//Update
itemDetail *item = [[itemDetail alloc]init];
item.strId = product[@"product_id"];
item.strName = product[@"name"];
item.strImage = product[@"image"];
[_arrayItem addObject:product];
NSLog(@"ITEM: %@", _arrayItem);
//NSLog(@" %@, %@, %@", item.strId, item.strName, item.strImage);
[_arraySubCategory addObject:product[@"name"]];
}
}
}
}
创建一个产品对象,将名称、图像等作为其属性。然后将此对象添加到您的数组中。
.
.
.
for (NSDictionary *product in subcategory[@"products"]) {
Product *product = [[Product alloc]init];
product.name = product[@"name"];
product.image = product[@"image"]; //and so on
[_arraySubCategory addObject:product];
}
现在,当您想从数组中获取数据时,
for (Product *product in _arraySubCategory)
{
if([product.name isEqualToString:@"abc"])
{
NSLog(@"%@",product.name);
//do what you want.
}
}