"Mantle" Parsing Dictionary with unknown keys

639 views Asked by At

I'm using Mantle Framework for a will now, and its simple and easy. But I'm currently facing a issue when I get a dictionary of dictionary, I'm not managing to parse them correctly.

For example :

{
    LSE =     {
        name = LSE;
        "stock_group_policies" =         {
            SET0 =             {
                definition =                 (
                                        {
                        high = "0.9999";
                        low = 0;
                        tick = "0.0001";
                    },
                                        {
                        high = "4.9995";
                        low = 1;
                        tick = "0.0005";
                    },
                                        {
                        high = "9.999";
                        low = 5;
                        tick = "0.001";
                    },
                                        {
                        high = "49.995";
                        low = 10;
                        tick = "0.005";
                    },
                                        {
                        high = "99.99";
                        low = 50;
                        tick = "0.01";
                    },
                                        {
                        high = "499.95";
                        low = 100;
                        tick = "0.05";
                    },
                                        {
                        high = "999.9";
                        low = 500;
                        tick = "0.1";
                    },
                                        {
                        high = "4999.5";
                        low = 1000;
                        tick = "0.5";
                    },
                                        {
                        high = 9999;
                        low = 5000;
                        tick = 1;
                    },
                                        {
                        high = "";
                        low = 10000;
                        tick = 5;
                    }
                );
                name = SET0;
                type = increments;
            };
            SET1 =             {
                definition =                 (
                                        {
                        high = "0.4999";
                        low = 0;
                        tick = "0.0001";
                    },
                                        {
                        high = "0.9995";
                        low = "0.5";
                        tick = "0.0005";
                    },
                                        {
                        high = "4.999";
                        low = 1;
                        tick = "0.001";
                    },
                                        {
                        high = "9.995";
                        low = 5;
                        tick = "0.005";
                    },
                                        {
                        high = "49.99";
                        low = 10;
                        tick = "0.01";
                    },
                                        {
                        high = "99.95";
                        low = 50;
                        tick = "0.05";
                    },
                                        {
                        high = "499.9";
                        low = 100;
                        tick = "0.1";
                    },
                                        {
                        high = "999.5";
                        low = 500;
                        tick = "0.5";
                    },
                                        {
                        high = 4999;
                        low = 1000;
                        tick = 1;
                    },
                                        {
                        high = 9995;
                        low = 5000;
                        tick = 5;
                    },
                                        {
                        high = "";
                        low = 10000;
                        tick = 10;
                    }
                );
                name = SET1;
                type = increments;
            };
        };
    };
    NASDAQ =     {
        name = NASDAQ;
        "stock_group_policies" =         {
        };
    };
    NYSE =     {
        name = NYSE;
        "stock_group_policies" =         {
        };
    };
    default =     {
        name = default;
        "stock_group_policies" =         {
        };
    };
}

I would like to end up with one dictionary, With a value of "exchanges" for every key : "SET0" "SET1" etc....

So I tried using this :

NSDictionary *exchanges = [MTLJSONAdapter modelOfClass:Exchange.class fromJSONDictionary:responseObject[@"exchanges"] error:&errorExchange];

But know luck there...

1

There are 1 answers

0
MCMatan On BEST ANSWER

This is what i finally did

+ (NSDictionary *) parseDictionayOfDictionarysOfModels: (Class) modelsClass
                                           dictionaryRow: (NSDictionary *) dictionaryRow {

    __block NSMutableDictionary *returnDictionary = [[NSMutableDictionary alloc] initWithCapacity:dictionaryRow.allKeys];


    [dictionaryRow enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSDictionary *dictionary, BOOL *stop) {

        NSError *error;
        MTLModel *model = [MTLJSONAdapter modelOfClass:modelsClass fromJSONDictionary:dictionary error:&error];

        returnDictionary[key] = model;

    }];

    return returnDictionary;
}