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

Posted

技术标签:

【中文标题】如何从 JSON 树中检索特定的 id【英文标题】:How to retrieve specific id from JSON tree 【发布时间】:2015-02-09 07:33:47 【问题描述】:

我可以在 tableView 中打印 sub_categoryproducts 并且工作正常,但我无法打印 id 的特定详细信息,例如如何打印 products_id=213 的详细信息得到name,image,model?

我的表格视图看起来像这样 [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"]];
          
      
  

【问题讨论】:

【参考方案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.
    

【讨论】:

对不起,您的意思是创建一个 NSObject 文件吗? 是的。 NSObject 文件将是数据模型“产品”。 您最好在产品模型中添加 productId 属性。然后与 id 而不是名称进行比较。 :) 你能给我更多细节@FawadMasud 吗?我创建了新的 NSObject 并调用了 itemDetail 和带有 id name image 的属性作为 NSString。现在我应该在哪里复制你的第一个代码? @FawadMasud _arraySubCategory 是 NSMutableArray

以上是关于如何从 JSON 树中检索特定的 id的主要内容,如果未能解决你的问题,请参考以下文章

检索 Firebase NoSQL/JSON 数据的最佳方式?

如何从事务中的特定查询中检索 last_insert_id?

如何从 Access 数据库 VB.net 中检索特定数据?

使用jackson从json数组中检索一个值

从子类别中检索特定的 JSON 密钥

如何从JSON请求解析特定数据