如何解析由JS发送到C的C字典中的字典?

时间:2015-09-23 05:55:51

标签: c pebble-watch pebble-sdk cloudpebble pebble-js

如何解析由JS发送到C的C字典中的字典? 下面是我试图解析的嵌套字典的示例数据和格式。

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);

我正在使用应用消息进行通信,因此当我在 inbox_received_callback 中的C中接收数据时,我尝试使用以下代码从字典中获取数据。

这是我尝试但不起作用的地方:

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);
}

这段代码不起作用,我觉得我在这里做错了什么:

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

但是我无法找到正确的做法。

1 个答案:

答案 0 :(得分:2)

我假设你正确地初始化了你的外部迭代器,那么内部的迭代器必须使用这样的东西:

DictionaryIterator iterator1;

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

dict_read_first只是重置回缓冲区的开头,但是如果我正确地读取文档,你需要dict_read_begin_from_buffer来初始化它。