将字典数组拆分为子数组

时间:2017-01-04 10:18:30

标签: ios objective-c nsmutablearray nsmutabledictionary

以下是我的JSON响应的一部分,其中'results'是一个字典数组:

{
  "results": [
    {
      "id": 6,
      "genre_name": "Action",
      "cover_image": "http://54.254.204.81/images/Action.png"
    },
    {
      "id": 5,
      "genre_name": "Adventure",
      "cover_image": "http://54.254.204.81/images/Adventure.png"
    },
    {
      "id": 4,
      "genre_name": "Romance",
      "cover_image": "http://54.254.204.81/images/Romance.png"
    },
    {
      "id": 3,
      "genre_name": "Sci-Fci",
      "cover_image": "http://54.254.204.81/images/Sci-Fi.png"
    },
    {
      "id": 1,
      "genre_name": "Guide",
      "cover_image": "http://54.254.204.81/images/Adventure_XHLbNfN.png"
    },
    {
      "id": 2,
      "genre_name": "Horror",
      "cover_image": "http://54.254.204.81/images/Phineas-and-Ferb-Christmas-Wallpaper.jpg"
    },
    {
      "id": 7,
      "genre_name": "Emotional",
      "cover_image": "http://54.254.204.81/images/a0fea991287cf41b6b9c4aa16196517f.jpg"
    },
    {
      "id": 8,
      "genre_name": "abcd",
      "cover_image": "http://54.254.204.81/images/logo_text_S0KyzUW.png"
    }
  ]
}

现在我有另一个JSON响应,其中'genres'是一个数组,其中包含具有键'id'的'results'数组对象子集的对象。

{
  "genres": [
    3,
    1
  ]
}

现在,我可以将“results”分成两个数组“results1”和“results2”,如:

{
  "results1": [
    {
      "id": 6,
      "genre_name": "Action",
      "cover_image": "http://54.254.204.81/images/Action.png"
    },
    {
      "id": 5,
      "genre_name": "Adventure",
      "cover_image": "http://54.254.204.81/images/Adventure.png"
    },
    {
      "id": 4,
      "genre_name": "Romance",
      "cover_image": "http://54.254.204.81/images/Romance.png"
    },
    {
      "id": 2,
      "genre_name": "Horror",
      "cover_image": "http://54.254.204.81/images/Phineas-and-Ferb-Christmas-Wallpaper.jpg"
    },
    {
      "id": 7,
      "genre_name": "Emotional",
      "cover_image": "http://54.254.204.81/images/a0fea991287cf41b6b9c4aa16196517f.jpg"
    },
    {
      "id": 8,
      "genre_name": "abcd",
      "cover_image": "http://54.254.204.81/images/logo_text_S0KyzUW.png"
    }
  ]
}

{
  "results2": [
    {
      "id": 3,
      "genre_name": "Sci-Fci",
      "cover_image": "http://54.254.204.81/images/Sci-Fi.png"
    },
    {
      "id": 1,
      "genre_name": "Guide",
      "cover_image": "http://54.254.204.81/images/Adventure_XHLbNfN.png"
    }
  ]
}

6 个答案:

答案 0 :(得分:2)

为此,您需要NSPredicateINNOT这样使用。

首先从两个JSON响应中创建resultArraygenresArray

NSArray *resultArray = [firstJSONResponse objectForKey:@"results"];
NSArray *genresArray = [secondJSONResponse objectForKey:@"genres"];

现在使用resultArray过滤您的NSPredicate以获得结果。

对于Result1

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (id IN %@)",genresArray];
NSArray *firstSplitArray = [resultArray filteredArrayUsingPredicate:predicate];
NSLog(@"%@",firstSplitArray);

对于Result2

predicate = [NSPredicate predicateWithFormat:@"id IN %@",genresArray];
NSArray *secondSplitArray = [resultArray filteredArrayUsingPredicate:predicate];
NSLog(@"%@",secondSplitArray);

答案 1 :(得分:2)

请按以下方式进行:

    NSMutableArray * genresArray = [NSMutableArray new];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (id IN %@)", genresArray];
    NSArray *filterArray = [Yourarray filteredArrayUsingPredicate:predicate];
    NSLog(@"filter:  %@",filterArray);

答案 2 :(得分:1)

在场景中试试这个:

NSDictionary *yourJson;
NSMutableArray *array1 = [[yourJson valueForKey:@"results"] mutableCopy];
NSMutableArray *array2 = [[yourJson valueForKey:@"results2"] mutableCopy];

for(int i =0 ; i<array2.count ; i++)
{
    NSString *objectFromarray2 = [array2 objectAtIndex:i];
    for(int j =0 ; j<array1.count ; j++)
    {
        NSDictionary *objectFromarray1 = [array2 objectAtIndex:i];
        if([[objectFromarray1 valueForKey:@"id"] isEqualToString:objectFromarray2])
        {
            [array2 removeObjectAtIndex:i];
            [array2 insertObject:objectFromarray1 atIndex:i];
            [array1 removeObject:objectFromarray1];
        }

    }

}

答案 3 :(得分:0)

是的,您可以使用NSPredicate执行此操作,但为此您首先必须将所有词典对象存储到一个NSArrayNSMutableArray中,以满足您的要求。

答案 4 :(得分:0)

喜欢:

NSString *search1 = @"3";
NSString *search2 = @"1";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(ANY SELF == %@) OR (ANY SELF == %@)", search1,search2];
NSArray *filtered = [Yourarray filteredArrayUsingPredicate:predicate];

答案 5 :(得分:0)

使用过滤器和地图

在Swift中做的一种方法 - 3
let resultArray = results["results"]!

let genArray = genres["genres"]!

var result1Array : [[String:Any]] = resultArray
var result2Array : [[String:Any]] = []

for gen in genArray {

    if let elm = resultArray.filter({ $0["id"] as! Int == gen }).first {
        result2Array.append(elm)
        result1Array = result1Array.filter({ $0["id"] as! Int != elm["id"] as! Int })
    }

}

print(["result2":result2Array])
print("\n")
print(["result1":result1Array])