过滤json重复数据

iOS 码拜 8年前 (2016-03-26) 1829次浏览
各位,本人想问一下怎么样根据json数据中的某个字段来进行过滤字段值相同的json数据中的值呢?好比本人下面的这段json数据,这段json数据是本人从一个完整的数据中拿出的一部分,而本人现在是想要根据这个json数据中的line_no这个字段来过滤数据,假如line_no数据中的值相同的话,那就进行随机的保留相同字段值的一个数据,说白了就是通过line_no来进行过滤重复的,而本人这边的json数据是通过NSLog(@”%@”,dicArray)打印出来的,现在就想做到过滤重复值

(
        {
        "line_direct" = 1;
        "line_no" = 0032;
        "location_x" = "118.379535";
        "location_y" = "31.3413317";
        "nearest_bus_no" = B18232;
        "nearest_station_no" = 24;
        "start_time" = "1899-12-30 06:00:00.0";
        "starting_name" = "\U56db\U8910\U5c71\U8857\U9053\U529e-1";
        "station_id" = 1961;
        "station_name" = "\U5e7f\U7535\U4e2d\U5fc3-1";
        "station_no" = 26;
        "stop_time" = "1899-12-30 19:30:00.0";
        "terminus_name" = "\U70df\U5382-1";
    },
        {
        "line_direct" = 1;
        "line_no" = 0021;
        "location_x" = "118.378685";
        "location_y" = "31.3393383";
        "nearest_bus_no" = B13813;
        "nearest_station_no" = 2;
        "start_time" = "1899-12-30 06:00:00.0";
        "starting_name" = "\U65b0\U5e02\U53e3-1";
        "station_id" = 2129;
        "station_name" = "\U5e7f\U7535\U4e2d\U5fc3-1";
        "station_no" = 6;
        "stop_time" = "1899-12-30 19:00:00.0";
        "terminus_name" = "\U8346\U5c71-1";
    },
        {
        "line_direct" = 1;
        "line_no" = 0031;
        "location_x" = "118.3788817";
        "location_y" = "31.339505";
        "nearest_bus_no" = 17387;
        "nearest_station_no" = 20;
        "start_time" = "1899-12-30 06:00:00.0";
        "starting_name" = "\U5229\U6c11\U4e1c\U8def\U9996\U672b\U7ad9-1";
        "station_id" = 3147;
        "station_name" = "\U5e7f\U7535\U4e2d\U5fc3-1";
        "station_no" = 22;
        "stop_time" = "1899-12-30 19:00:00.0";
        "terminus_name" = "\U706b\U8f66\U7ad9\U6c7d\U8f66\U7ad9-1";
    },
        {
        "line_direct" = 1;
        "line_no" = 0032;
        "location_x" = "118.3787767";
        "location_y" = "31.3395";
        "nearest_bus_no" = B12142;
        "nearest_station_no" = 14;
        "start_time" = "1899-12-30 06:00:00.0";
        "starting_name" = "\U7696\U5357\U533b\U5b66\U9662\U5357-1";
        "station_id" = 3254;
        "station_name" = "\U5e7f\U7535\U4e2d\U5fc3-1";
        "station_no" = 19;
        "stop_time" = "1899-12-30 19:00:00.0";
        "terminus_name" = "\U6e2f\U4e00\U8def\U516c\U4ea4\U9996\U672b\U7ad9-1";
    }
)
解决方案

80

给你推荐一个扩展可以实现你要的功能。
LinqToObjectiveC
去重复的使用

NSArray* names = @[@"bill", @"bob", @"bob", @"brian", @"bob"];
NSArray* distinctNames = [names linq_distinct];  // returns bill, bob and brian
////假如是distinct一个dictionary中元素,可以如下这样
NSArray* peopleWithUniqueAges = [input linq_distinct:^id(id person) {
    return [person age];
}];

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明过滤json重复数据
喜欢 (0)
[1034331897@qq.com]
分享 (0)