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

问题描述 投票:2回答:3

数组格式:

{
    "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 = color和value = 7)AND(attribute_code = size和value = 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
ios nsarray nsdictionary nspredicate nscompoundpredicate
3个回答
0
投票

你必须这样做。检查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'是您提供的String。我刚刚将NSJSONSerialization转换为将Stirng转换为NSDictionary。


0
投票

您必须将两个过滤器与OR而不是AND合并。它永远不会与你的情况下的AND相比,并将返回0个对象作为filteredArray

这是你的custom_attributes数组的复合谓词。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(attribute_code ==[c] 'color' AND value == '7') OR (attribute_code ==[c] 'size' AND value == '12')"];
// Assuming that you have parsed you custom_attributes object to customAtrributes

NSArray *filteredArray = [customAtrributes filteredArrayUsingPredicate:predicate];

0
投票

尝试使用ANY。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"custom_attributes.value == %d", 7];
NSArray *filterArray = [yourArray filteredArrayUsingPredicate:predicate];
NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"custom_attributes.attribute_code CONTAINS[c] %@", "color"];
NSArray *filterArray1 = [filterArray filteredArrayUsingPredicate:predicate1];
© www.soinside.com 2019 - 2024. All rights reserved.