如何从JSON树中检索特定的id

时间:2015-02-09 07:33:47

标签: ios json nsdictionary

我可以在tableView中打印sub_categoryproducts,但它工作正常,但我无法打印id的具体细节,例如如何打印{的详细信息{1}} = products_id获取213nameimage

我的表格视图如下[categories> name],然后是[sub_category> name],然后我需要所选的products_id modelnameimage

我正在使用segue

我的JSON:

model

。 。

我的代码:

{"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"}

1 个答案:

答案 0 :(得分:1)

创建一个名称,图像等作为其属性的产品对象。然后将此对象添加到数组中。

.
.
.
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.
    }
}