如何解析JS传给C的Dictionary of dictionary? 下面是我试图解析的嵌套字典的示例数据和格式。
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);
我正在使用 app message 进行通信,所以当我在 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;
但我无法找出正确的方法来做到这一点。
最佳答案
我假设你正确地初始化了你的外部迭代器,那么内部迭代器就必须使用这样的东西:
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 来初始化它。
关于c - 如何解析JS发送给C的C中字典的Dictionary?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32732001/