2015-09-23 56 views
1

如何解析C中由JS發送給C的字典字典? 下面是我試圖解析的嵌套字典的示例數據和格式。如何解析由C發送的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); 

我使用應用消息進行通信,所以當我用C收到內部inbox_received_callback數據,我曾嘗試下面的代碼來獲取數據了字典。

這是我試過,但沒有工作:

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; 

,但我無法弄清楚正確的做法。

回答

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初始化它,如果我正確閱讀文檔。