比较NSDictionary中的相同键值

时间:2021-07-26 13:04:54

I have a JSON result that consist in NSmutableArray with Dictionarys, and I want to know how iterate and compare identical key values (id_sucursal in my case) and to save it in Array to create cluster annotation. Below is the JSON result:

我有一个JSON结果,它包含在带有Dictionarys的NSmutableArray中,我想知道如何迭代和比较相同的键值(在我的情况下为id_sucursal)并将其保存在Array中以创建集群注释。以下是JSON结果:

    {
    ancla = 1;
    categoria = 12;
    descripcion = "Gur\U00fa Ofertas te trae un 15% de descuento* en todos los platos o un postre* de cortes\U00eda en el Centro Cultural Mediterr\U00e1neo La Gloria";
    descuento = 16;
    "id_cupon" = 540;
    "id_sucursal" = 45;
    lat = "4.63323";
    lon = "-74.144474";
    premium = "<null>";
    rutaimagen = "ruta/imagen/14";
    tipooferta = "% Descuento";
    titulo = "Decoraciones del hogar Home Center";
    zona = 2;
},
    {
    ancla = "<null>";
    categoria = 4;
    descripcion = "$50.000 en vez de $120.000 por noche romantica para dos personas. Incluye cena romantica+champa\U00f1a+acomodaci\U00f3n en cama doble";
    descuento = 61;
    "id_cupon" = 536;
    "id_sucursal" = 50;
    lat = "4.669106";
    lon = "-74.043949";
    premium = 1;
    rutaimagen = "ruta/imagen/7";
    tipooferta = "% Descuento";
    titulo = "Lavado de autos Splash";
    zona = 1;
},
    {
    ancla = "<null>";
    categoria = 3;
    descripcion = "Durante el mes de septiembre lleva uno de nuestros colchones Elegance por tan solo $429.000. Precio normal: $858.000. Te lo llevamos hasta la puerta de tu casa \U00a1y no te cuesta m\U00e1s!";
    descuento = 51;
    "id_cupon" = 537;
    "id_sucursal" = 54;
    lat = "4.655775";
    lon = "-74.099317";
    premium = 1;
    rutaimagen = "ruta/imagen/11";
    tipooferta = "% Descuento";
    titulo = "Lavado de autos Splash";
    zona = 1;
},
    {
    ancla = "<null>";
    categoria = 4;
    descripcion = "$240.0000 en ves de $480.000 por  3 dias y 4 noche en el Hotel Hilton de Bogot\U00e1 en acomodaci\U00f3n doble";
    descuento = 51;
    "id_cupon" = 535;
    "id_sucursal" = 45;
    lat = "4.63323";
    lon = "-74.144474";
    premium = 1;
    rutaimagen = "ruta/imagen/2";
    tipooferta = "% Descuento";
    titulo = "Lavado de autos Splash";
    zona = 1;
}

2 个解决方案

#1


2  

If you want unique NSDictionary objects from the array, then I suggest you use NSSet instead of NSArray.

如果你想从数组中获得唯一的NSDictionary对象,那么我建议你使用NSSet而不是NSArray。

This will help you to add only unique objects to the NSSet. It is very easy to use and the below link is something you want because I resolved such a case using NSMutableSet :

这将帮助您仅向NSSet添加唯一对象。它非常容易使用,下面的链接是你想要的,因为我使用NSMutableSet解决了这种情况:

NSSet Tutorial

NSSet教程

#2


0  

After you change JSON to Objective C structures, for example using:

将JSON更改为Objective C结构后,例如使用:

NSArray *mainArray = [NSJSONSerialization JSONObjectWithData: [jsonString dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableContainers error:nil];

You can iterate like that:

你可以这样迭代:

NSMutableArray* duplicates = [NSMutableArray array]; //keeping duplicated entries
NSMutableArray* uniques = [NSMutableArray array]; //to keep track on which ids where visited

for (NSDictionary* dict in mainArray)
{
    NSNumber* id_sucursal = [dict objectForKey:@"id_sucursal"]; //get id of current element
    BOOL isDuplicated = NO;

    for (NSNumber* uniqueId in uniques) //for all visited ids
    {
        if([id_sucursal isEqualToNumber:uniqueId]) //if same id was already found before
        {
            isDuplicated = YES;
            break;
        }
    }

    if(isDuplicated)
    {
         [duplicates addObject:dict]; //add element to duplicated list
    }
    else 
    { 
         [uniques addObject:id_sucursal]; //add id to visited ids 
    }
}

#1


2  

If you want unique NSDictionary objects from the array, then I suggest you use NSSet instead of NSArray.

如果你想从数组中获得唯一的NSDictionary对象,那么我建议你使用NSSet而不是NSArray。

This will help you to add only unique objects to the NSSet. It is very easy to use and the below link is something you want because I resolved such a case using NSMutableSet :

这将帮助您仅向NSSet添加唯一对象。它非常容易使用,下面的链接是你想要的,因为我使用NSMutableSet解决了这种情况:

NSSet Tutorial

NSSet教程

#2


0  

After you change JSON to Objective C structures, for example using:

将JSON更改为Objective C结构后,例如使用:

NSArray *mainArray = [NSJSONSerialization JSONObjectWithData: [jsonString dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableContainers error:nil];

You can iterate like that:

你可以这样迭代:

NSMutableArray* duplicates = [NSMutableArray array]; //keeping duplicated entries
NSMutableArray* uniques = [NSMutableArray array]; //to keep track on which ids where visited

for (NSDictionary* dict in mainArray)
{
    NSNumber* id_sucursal = [dict objectForKey:@"id_sucursal"]; //get id of current element
    BOOL isDuplicated = NO;

    for (NSNumber* uniqueId in uniques) //for all visited ids
    {
        if([id_sucursal isEqualToNumber:uniqueId]) //if same id was already found before
        {
            isDuplicated = YES;
            break;
        }
    }

    if(isDuplicated)
    {
         [duplicates addObject:dict]; //add element to duplicated list
    }
    else 
    { 
         [uniques addObject:id_sucursal]; //add id to visited ids 
    }
}