ios - 如何从 JSON 树中检索特定的 ID

标签 ios json nsdictionary

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

我的表格 View 看起来像这样 [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.
    }
}

关于ios - 如何从 JSON 树中检索特定的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28404848/

相关文章:

objective-c - 按 NSDictionary 值对 NSArray 进行排序

javascript - 从 json 获取值到 angularjs 对象

json - 在 Elm 应用程序中使用静态数据 : parse JSON or use Elm values?

ios - 使用 Swift AVPlayer 显示视频轨道的时间码

iphone - iOS 开发 : How can I encapsulate a string in an NSData object?

json - 统计属性为 "null"或包含 "null"的对象数量

ios - 清除复制的字典

iphone - 具有多个 objectKeys 的 NSMutableArray 中的 NSDictionary 上的 sortUsingComparator

iphone - iOS App 内用户的密码/身份验证

ios - Ntimer 倒计时跳过按钮