2015-02-09 55 views
0

我可以在tableView中打印sub_categoryproducts,它工作正常,但無法打印id的特定詳細信息,例如,如何打印​​= 213的詳細信息得到nameimagemodel如何檢索JSON樹中的特定id

我的表視圖看起來像那麼我需要選擇該[類別>名稱],然後[sub_category>名稱] products_id nameimagemodel

我使用賽格瑞

我的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"]]; 
      } 
     } 
    } 
} 

回答

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

對不起你的意思是創建一個NSObject的文件? – 2015-02-09 07:50:39

+0

是的。 NSObject文件將成爲數據模型「Product」。 – 2015-02-09 07:51:38

+0

您應該在產品模型中更好地添加productId屬性。然後與id不是名稱比較。 :) – 2015-02-09 07:55:04

相關問題