(json-c學習11)解析嵌套的數組

其它什麼頭文件,編譯什麼的看前面的部分就好。 

代碼如下:

661 void ParsingTest(void)
662 {
663     int fatherLen = 0;
664     int sonLen = 0;
665     const char *str = "[{\"gp\":1,\"body\":[{\"ln\":1,\"sd\":2},{\"ln\":2,\"sd\":3}]},{\"gp\":3,\"body\":[{\"ln\":6,\"sd\":7},{\"ln\":8,\"sd\":9},{\"ln\"    :12,\"sd\":13}]}]";
666     json_object *arrayObj = NULL;
667     json_object *sonArrayObj = NULL;
668     arrayObj = json_tokener_parse(str);
669     if (NULL == arrayObj)
670     {
671         printf("%s : json_tokener_parse failed!\n", __FUNCTION__);
672         return;
673     }
674     else
675     {
676         if (json_object_get_type(arrayObj) != json_type_array)
677         {
678             json_object_put(arrayObj);
679             printf("this is is an error array json!\n");
680             return ;
681         }
682         else
683         {
684             fatherLen = json_object_array_length(arrayObj);
685             printf("fatherLen = %d\n", fatherLen);
686             int i = 0;
687             json_object *pstObj = NULL;
688             for (;i < fatherLen; i++)
689             {
690                 pstObj = json_object_array_get_idx(arrayObj, i);
691                 json_object_object_foreach(pstObj, key1, val1)
692                 {
693                     if (0 == strcmp(key1, "gp"))
694                     {
695                         int gp = json_object_get_int(val1);
696                         printf("gp = %d\n", gp);
697                     }
698                     else if (0 == strcmp(key1, "body"))
699                     {
700                         sonLen = json_object_array_length(val1);
701                         printf("sonLen = %d\n", sonLen);
702                         int j = 0;
703                         int ln = 0;
704                         int sd = 0;
705                         json_object *sonPstObj = NULL;
706                         for (;j < sonLen; j++)
707                         {
708                             sonPstObj = json_object_array_get_idx(val1, j);
709                             json_object_object_foreach(sonPstObj, key2, val2)
710                             {
711                                 if (0 == strcmp(key2, "ln"))
712                                 {
713                                     ln = json_object_get_int(val2);
714                                     printf("ln = %d\n", ln);
715                                 }
716                                 else if (0 == strcmp(key2, "sd"))
717                                 {
718                                     sd = json_object_get_int(val2);
719                                     printf("sd = %d\n", sd);
720                                 }
721                             }
722                         }
723                     }
724                 }
725             }
726         }
727     }
728     json_object_put(arrayObj);
729 }

執行結果:

fatherLen = 2
gp = 1
sonLen = 2
ln = 1
sd = 2
ln = 2
sd = 3
gp = 3
sonLen = 3
ln = 6
sd = 7
ln = 8
sd = 9
ln = 12
sd = 13

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章