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

Posted

技术标签:

【中文标题】如何在嵌套字典数组上使用 NSpredicate【英文标题】:How to use NSpredicate on array of nested dictionaries 【发布时间】:2015-12-23 13:37:37 【问题描述】:

数组格式:


    "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 and value = 7)AND(attribute_code = size and 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

【问题讨论】:

您有 2 个值要比较,因此一次只能选择 1 个过滤器,您可以搜索 color7size12。两者不可能同时进行。更正你的问题。 【参考方案1】:

你必须这样做。检查 filterArrayNSLog

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' 是您提供的字符串。我刚刚做了 NSJSONSerializationStirng 转换为 NSDictionary

【讨论】:

【参考方案2】:

您必须将两个过滤器与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];

【讨论】:

【参考方案3】:

尝试使用 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];

【讨论】:

以上是关于如何在嵌套字典数组上使用 NSpredicate的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Jersey 将嵌套列表编组为 JSON?我得到一个空数组或一个包含数组的单元素字典数组

如何将嵌套字典转换为 3D 数组

如何解析 json 值的动态变化。带有 2 个嵌套 dic 的字典,然后是数组,然后是 dic

数据过滤 Javascript(嵌套字典和数组)

如何在 swift 中使用嵌套字典发送 POST 请求

Swift & Firestore - 使用 .arrayUnion() 将字典附加到嵌套数组