在字典 Swift iOS 内的字典数组中过滤 json 响应

Filter json response in array of dictionary inside dictionary Swift iOS

我收到 NSDictionary 产品列表的响应

 {
"products": {
    "title": "Chair",
    "regular_price": "2.22",
    "dimensions": {
        "width": "",
        "height": "",
        "length": ""
    },
    "attributes": [{
        "options": ["11\/30\/2016"],
        "name": "Arrival Date"
    }, {
        "options": ["Black"],
        "name": "Color"
    }],
    "categories": ["28"]
}

}.......

使用 NSPredicate 我可以使用

过滤包含值 "Chair" 的产品
let namepredicate = NSPredicate(format: "title == Chair")
           self.filteredProducts = (self.product).filteredArrayUsingPredicate(namepredicate)

但是我如何过滤 "Color", "Black" 里面的属性和 "Black" 在另一个数组里(Swift)?

首先,将self.product重命名为self.products。它是多个产品的数组,相应地命名它。

您可以将现有的 NSPredicate 混乱替换为:

self.filteredProducts = self.products.filter{ [=10=]["title"] == "Chair" }

您可以像这样按颜色过滤:

self.filteredProducts = self.products.filter{ product in
    return product["attributes"]?.contains{ attribute in
        return attribute["name"] == "Color" &&
               attribute["options"]?.contains("Black") ?? false
    } ?? false
}