ios - 如何在嵌套字典数组上使用NSpredicate

标签 ios nsarray nsdictionary nspredicate nscompoundpredicate

数组格式:

{
    "sku": "NikeL101Black",
    "name": "Nike Black shirt -L",
    "attribute_set_id": 4,
    "price": 30,
    "status": 1,
    "visibility": 1,
    "type_id": "simple",
    "created_at": "2015-12-01 23:02:07",
    "updated_at": "2015-12-01 23:02:23",
    "weight": 2,
    "product_links": [],
    "options": [],
    "tier_prices": [],
    "custom_attributes": [
      {
        "attribute_code": "swatch_image",
        "value": "/m/i/miler-uv-mens-t-shirt-black-p7394-6131_zoom_1_1.jpg"
      },
      {
        "attribute_code": "tax_class_id",
        "value": "2"
      },
      {
        "attribute_code": "image",
        "value": "/m/i/miler-uv-mens-t-shirt-black-p7394-6131_zoom_1_1.jpg"
      },
      {
        "attribute_code": "category_ids",
        "value": [
          "3"
        ]
      },
      {
        "attribute_code": "description",
        "value": "<p>Cool black nike tshirt</p>"
      },
      {
        "attribute_code": "color",
        "value": "7"
      },
      {
        "attribute_code": "required_options",
        "value": "0"
      },
      {
        "attribute_code": "size",
        "value": "14"
      },
      {
        "attribute_code": "has_options",
        "value": "0"
      },
      {
        "attribute_code": "vendor",
        "value": "Paxcel Cloth House"
      },
      {
        "attribute_code": "small_image",
        "value": "/m/i/miler-uv-mens-t-shirt-black-p7394-6131_zoom_1_1.jpg"
      },
      {
        "attribute_code": "thumbnail",
        "value": "/m/i/miler-uv-mens-t-shirt-black-p7394-6131_zoom_1_1.jpg"
      },
      {
        "attribute_code": "url_key",
        "value": "nike-red-shirt-s-7"
      },
      {
        "attribute_code": "meta_title",
        "value": "Nike Red shirt -S"
      },
      {
        "attribute_code": "meta_keyword",
        "value": "Nike Red shirt -S"
      },
      {
        "attribute_code": "meta_description",
        "value": "Nike Red shirt -S <p>Cool red noke tshirt</p>"
      },
      {
        "attribute_code": "options_container",
        "value": "container2"
      }
    ]
  },

如何通过(attribute_code =颜色和值= 7)和(attribute_code =大小和值= 12)过滤上述类型字典的数组

我尝试了一个复合谓词:
NSPredicate *filterChildrenBySize = [NSPredicate predicateWithFormat:@"ANY custom_attributes.attribute_code == size AND custom_attributes.value.integerValue == %@", [[attributes objectForKey:@"sizeInfo"] objectForKey:kAttributeCodeSize]];

Predicate looks like this --> ANY custom_attributes.attribute_code == color AND custom_attributes.value.integerValue == 4

最佳答案

您必须这样做。检查 filterArray NSLog

NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil];
NSLog(@"OUTPUT DATA: %@" ,jsonDict);
NSArray *arrayList = [jsonDict objectForKey:@"custom_attributes"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.attribute_code == 'color' AND self.value == '7'"];
NSArray *filterArray = [arrayList filteredArrayUsingPredicate:predicate];
NSLog(@"FILTERED DATA: %@" ,filterArray);

这里的“ jsonString ”是您提供的字符串。我只是做了 NSJSONSerialization ,将 Stirng 转换为 NSDictionary

关于ios - 如何在嵌套字典数组上使用NSpredicate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34436932/

相关文章:

ios - MKMapView 双击

ios - NSArray 对字符串的另一个 NSArray 进行排序

objective-c - 提高快速枚举性能

ios - 从 NSDictionary 中删除键

ios - 我的 NSJSONSerialization 似乎给了我嵌套的 NSDictionaries - 我如何访问外部键中的特定键?

javascript - 方向更改事件后,Chrome iOS 87.0.4280.77 中的 window.innerWidth 不正确

ios 使用 mfMailComposer 发送 docx 附件失败

ios - Firebase 3x 方法不适用于真实设备但适用于模拟器 Swift 3.0

crash - NSArray 可能导致应用程序崩溃?

ios - 为什么某些对象不能与 NSDictionary 速记一起使用?