(json-c學習9)讀寫json格式的文件

讀寫的文件,有普通的json格式的文件,還有數組形式的json格式的文件。

讀普通的文件使用:
guoyanzhang@debian:~/test$ cat test.json
{"group":1,"test":2}

讀數組形式的文件使用:
guoyanzhang@debian:~/test$ cat test1.json
[{"group":1,"test":2},{"group":2,"test":3},{"group":12,"test":13},{"group":12,"test":13}]

代碼如下:

普通文件:

444 void TestReadJsonFile_object(void)
445 {
446     char *file = "./test.json";
447     json_object *pstObj = NULL;
448     json_object *sonPstObj = NULL;
449     pstObj = json_object_from_file(file);
450
451     int group = 0;
452     int test = 0;
453     if (NULL == pstObj)
454     {
455         printf("%s : json_tokener_parse failed.\n", __FUNCTION__);
456         return;
457     }
458     else
459     {
460         //if (json_object_get_type(pstObj) != json_type_array)
461         if (json_object_get_type(pstObj) != json_type_object)
462         {
463             json_object_put(pstObj);
464             printf("an error json string!\n");
465             return;
466         }
467         printf("%p\n", pstObj);
468         json_object_object_foreach(pstObj, key1, val1)
469         {
470             if (0 == strcmp(key1, "group"))
471             {
472                 group = json_object_get_int(val1);
473                 printf("group = %d\n", group);
474             }
475             if (0 == strcmp(key1, "test"))
476             {
477                 test = json_object_get_int(val1);
478                 printf("test = %d\n", test);
479             }
480         }
481         json_object_put(pstObj);
482     }
483
484 }

數組文件:

486 void TestReadJsonFile_array(void)
487 {
488     char *file = "./test1.json";
489     json_object *pstObj = NULL;
490     json_object *sonPstObj = NULL;
491     pstObj = json_object_from_file(file);
492
493     int group = 0;
494     int test = 0;
495     if (NULL == pstObj)
496     {
497         printf("%s : json_tokener_parse failed.\n", __FUNCTION__);
498         return;
499     }
500     else
501     {
502         json_object *pValue = NULL;
503
504         json_object *pObj = json_object_new_object();
505         pValue = json_object_new_int(12);
506         json_object_object_add(pObj, "group", pValue);
507         pValue = json_object_new_int(13);
508         json_object_object_add(pObj, "test", pValue);
509
510         json_object_array_add(pstObj, pObj);  //將對象加入數組
511
512
513         json_object_to_file(file, pstObj);
514         #if 1
515         if (json_object_get_type(pstObj) != json_type_array)
516         //if (json_object_get_type(pstObj) != json_type_object)
517         {
518             json_object_put(pstObj);
519             printf("an error json string!\n");
520             return;
521         }
522         //printf("%p\n", pstObj);
523         int count = json_object_array_length(pstObj);
524         printf("count = %d\n", count);
525         int i = 0;
526         struct json_object *arrayObj = NULL;
527         for (;i < count; i++)
528         {
529             arrayObj = json_object_array_get_idx(pstObj, i);
530             json_object_object_foreach(arrayObj, key1, val1)
531             {
532                 if (0 == strcmp(key1, "group"))
533                 {
534                     group = json_object_get_int(val1);
535                     printf("group = %d\n", group);
536                 }
537                 if (0 == strcmp(key1, "test"))
538                 {
539                     test = json_object_get_int(val1);
540                     printf("test = %d\n", test);
541                 }
542             }
543
544         }
545         #endif
546         json_object_put(pstObj);
547     }

代碼運行結果:

-----------------------------------------------------
0x5567961aacd0
group = 1
test = 2
-----------------------------------------------------
count = 4
group = 1
test = 2
group = 2
test = 3
group = 12
test = 13
group = 12
test = 13

 

參考:https://blog.csdn.net/toddor_c/article/details/7268020

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