json-c 读写文件

来源:互联网 发布:mac os 最新版本 .cdr 编辑:程序博客网 时间:2024/05/16 07:46

jsonc 写配置文件比较简单,并且解析配置文件也比较省事。

写配置文件:

[cpp] view plaincopy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <stddef.h>  
  4. #include <string.h>  
  5.    
  6. #include "./inc/json.h"  
  7. #define CONFIG_FILE "config.json"  
  8. typedef struct{  
  9.     int len3;  
  10.     int len4;  
  11. }A_A_s;  
  12. typedef struct {  
  13.     char name[100];  
  14.     int len;  
  15.     A_A_s A1_1;  
  16. }A_s;  
  17.   
  18. typedef struct {  
  19.     int B_len;  
  20.     int B_len3;  
  21. }B_s;  
  22.   
  23. typedef struct {  
  24.     int C_len;  
  25.     int C_len3;  
  26. }C_s;  
  27.   
  28. typedef struct {  
  29.     int root1;  
  30.     int root2;  
  31.     char token[100];  
  32.     A_s A1;  
  33.     B_s B1;  
  34.     C_s C1;  
  35.   
  36. }root_s;   
  37. int main(int argc, char **argv)  
  38. {  
  39.     struct json_object *new_obj;  
  40.     struct json_object *root_obj;  
  41.   
  42.     MC_SET_DEBUG(1);  
  43.   
  44.     root_s config_struct =   
  45.     {  
  46.         .root1 = 1,  
  47.         .root2 = 2,  
  48.         .token = "token1",  
  49.         .A1 =   
  50.         {  
  51.             .name = "A_name_1",  
  52.             .len = 101,  
  53.             .A1_1 =   
  54.             {   .len3 = 1001,  
  55.                 .len4 = 1002,  
  56.             },  
  57.               
  58.         },  
  59.         .B1 =   
  60.         {  
  61.             .B_len = 201,  
  62.             .B_len3 = 202,  
  63.         },  
  64.         .C1 =   
  65.         {  
  66.             .C_len = 301,  
  67.             .C_len3 = 302,  
  68.         },  
  69.   
  70.     };  
  71.    
  72.     root_obj = json_object_new_object();   
  73.   
  74.     new_obj = json_object_new_int(config_struct.root1);  
  75.     json_object_object_add(root_obj,"L1_root1",new_obj);  
  76.    
  77.     new_obj = json_object_new_int(config_struct.root2);  
  78.     json_object_object_add(root_obj,"L1_root2",new_obj);  
  79.    
  80.     new_obj = json_object_new_string(config_struct.token);  
  81.     json_object_object_add(root_obj,"L1_root_token",new_obj);  
  82.       
  83.     json_object *json_obj_A,*json_obj_B,*json_obj_C,*json_obj_A_A1;  
  84.     json_obj_A = json_object_new_object();   
  85.     json_obj_B = json_object_new_object();   
  86.     json_obj_C = json_object_new_object();   
  87.     json_obj_A_A1 = json_object_new_object();   
  88.       
  89.     json_object_object_add(json_obj_A_A1,"A_A1_len3",json_object_new_int(config_struct.A1.A1_1.len3));  
  90.     json_object_object_add(json_obj_A_A1,"A_A1_len4",json_object_new_int(config_struct.A1.A1_1.len4));  
  91.   
  92.     json_object_object_add(json_obj_A,"A_name",json_object_new_string(config_struct.A1.name));  
  93.     json_object_object_add(json_obj_A,"A_len",json_object_new_int(config_struct.A1.len));  
  94.     json_object_object_add(json_obj_A,"A_A1_Struct",json_obj_A_A1);  
  95.   
  96.     json_object_object_add(json_obj_B,"B_len",json_object_new_int(config_struct.B1.B_len));  
  97.     json_object_object_add(json_obj_B,"B_len3",json_object_new_int(config_struct.B1.B_len3));  
  98.   
  99.     json_object_object_add(json_obj_C,"C_len",json_object_new_int(config_struct.C1.C_len));  
  100.     json_object_object_add(json_obj_C,"C_len3",json_object_new_int(config_struct.C1.C_len3));  
  101.   
  102.     json_object_object_add(root_obj,"L1_A1",json_obj_A);  
  103.     json_object_object_add(root_obj,"L1_B1",json_obj_B);  
  104.     json_object_object_add(root_obj,"L1_C1",json_obj_C);  
  105.       
  106.     json_object_to_file(CONFIG_FILE,root_obj);  
  107.     json_object_put(root_obj);  
  108.     json_object_put(json_obj_A);  
  109.     json_object_put(json_obj_A_A1);  
  110.     json_object_put(json_obj_B);  
  111.     json_object_put(json_obj_C);  
  112.     json_object_put(new_obj);  
  113.   
  114.     return 0;  
  115. }  
[cpp] view plaincopy
  1. 写完的配置文件如下:  
[html] view plaincopy
  1. {  
  2.     "L1_root1": 1,  
  3.     "L1_root2": 2,  
  4.     "L1_root_token": "token1",  
  5.     "L1_A1": {  
  6.         "A_name": "A_name_1",  
  7.         "A_len": 101,  
  8.         "A_A1_Struct": {  
  9.             "A_A1_len3": 1001,  
  10.             "A_A1_len4": 1002  
  11.         }  
  12.     },  
  13.     "L1_B1": {  
  14.         "B_len": 201,  
  15.         "B_len3": 202  
  16.     },  
  17.     "L1_C1": {  
  18.         "C_len": 301,  
  19.         "C_len3": 302  
  20.     }  
  21. }  

 

符合期望的东西。

json_object_object_add 该动作如果要添加的Key已经存在,则修改之,否则才会添加。

所以想要修改配置文件,直接用add的方法就好了,不要用del+add的方式修改。

0 0