How to parse Dictionary of dictionary in C which is sent by JS to C?

778 Views Asked by At

How to parse Dictionary of dictionary in C which is sent by JS to C? Below is the sample data and format of nested dictionary which I am trying to parse.

var temp_DATA_CONTAINER =  {'KEY_1':"abc", 'KEY_2':"bcd", 'KEY_1':"efg"};
var outer_dictionary  =  {'OUTER_KEY' : temp_DATA_CONTAINER};      
Pebble.sendAppMessage(outer_dictionary);

I am using app message for communication, so when I receive data in C inside inbox_received_callback, I have tried below code to get data out of dictionary.

This is what I tried but not working:

Tuple *t = dict_read_first(iterator);
while (t != NULL)
{
    switch (t->key)
    {
    case OUTER_KEY:
        {
             DictionaryIterator *iterator1 = (DictionaryIterator *)t->value->data;
                 Tuple *tuple1 = dict_read_first(iterator1);

                    while(tuple1 != NULL)
                    {
                            switch(tuple1->key)
                            {
                              case KEY_1:
                              {
                                 printf("~~ In key 1  ");
                                break;
                              }
                              case KEY_2:
                              {
                                 printf("~~In key  2");
                                break;
                              }
                              case KEY_3:
                              {
                                 printf("~~In key  3");
                                break;
                             }
                         }
                       // Get next pair, if any
                      tuple1 = dict_read_next(iterator1);
                    }   
        }
}

 t = dict_read_next(iterator);
}

This code is not working, I think I doing something wrong here:

DictionaryIterator *iterator1 = (DictionaryIterator *)t->value->data; 

but I'm not able to figure out the right approach to do that.

1

There are 1 best solutions below

0
Dale On

I'm assuming you initialized your outer iterator correctly, so then the inner one would have to use something like this:

DictionaryIterator iterator1;

Tuple *tuple = dict_read_begin_from_buffer(&iterator1, t->value->data, strlen(t->value->data));

dict_read_first simply resets back to the beginning of the buffer, but you need dict_read_begin_from_buffer to initialize it, if I am reading the docs correctly.