[英]How to use the NSPredicate for searching a string inside the array in dictionary, in array of dictionaries?
[英]How to use NSpredicate on array of nested dictionaries
數組格式:
{
"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)AND(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 。
您必須使用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];
嘗試為此使用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];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.