json-c 读写文件

时间:2025-03-28 17:09:47
  • #include <>  
  • #include <>  
  • #include <>  
  • #include <>  
  •    
  • #include "./inc/"  
  • #define CONFIG_FILE ""  
  • typedef struct{  
  •     int len3;  
  •     int len4;  
  • }A_A_s;  
  • typedef struct {  
  •     char name[100];  
  •     int len;  
  •     A_A_s A1_1;  
  • }A_s;  
  •   
  • typedef struct {  
  •     int B_len;  
  •     int B_len3;  
  • }B_s;  
  •   
  • typedef struct {  
  •     int C_len;  
  •     int C_len3;  
  • }C_s;  
  •   
  • typedef struct {  
  •     int root1;  
  •     int root2;  
  •     char token[100];  
  •     A_s A1;  
  •     B_s B1;  
  •     C_s C1;  
  •   
  • }root_s;   
  • int main(int argc, char **argv)  
  • {  
  •     struct json_object *new_obj;  
  •     struct json_object *root_obj;  
  •   
  •     MC_SET_DEBUG(1);  
  •   
  •     root_s config_struct =   
  •     {  
  •         .root1 = 1,  
  •         .root2 = 2,  
  •         .token = "token1",  
  •         .A1 =   
  •         {  
  •             .name = "A_name_1",  
  •             .len = 101,  
  •             .A1_1 =   
  •             {   .len3 = 1001,  
  •                 .len4 = 1002,  
  •             },  
  •               
  •         },  
  •         .B1 =   
  •         {  
  •             .B_len = 201,  
  •             .B_len3 = 202,  
  •         },  
  •         .C1 =   
  •         {  
  •             .C_len = 301,  
  •             .C_len3 = 302,  
  •         },  
  •   
  •     };  
  •    
  •     root_obj = json_object_new_object();   
  •   
  •     new_obj = json_object_new_int(config_struct.root1);  
  •     json_object_object_add(root_obj,"L1_root1",new_obj);  
  •    
  •     new_obj = json_object_new_int(config_struct.root2);  
  •     json_object_object_add(root_obj,"L1_root2",new_obj);  
  •    
  •     new_obj = json_object_new_string(config_struct.token);  
  •     json_object_object_add(root_obj,"L1_root_token",new_obj);  
  •       
  •     json_object *json_obj_A,*json_obj_B,*json_obj_C,*json_obj_A_A1;  
  •     json_obj_A = json_object_new_object();   
  •     json_obj_B = json_object_new_object();   
  •     json_obj_C = json_object_new_object();   
  •     json_obj_A_A1 = json_object_new_object();   
  •       
  •     json_object_object_add(json_obj_A_A1,"A_A1_len3",json_object_new_int(config_struct.A1.A1_1.len3));  
  •     json_object_object_add(json_obj_A_A1,"A_A1_len4",json_object_new_int(config_struct.A1.A1_1.len4));  
  •   
  •     json_object_object_add(json_obj_A,"A_name",json_object_new_string(config_struct.));  
  •     json_object_object_add(json_obj_A,"A_len",json_object_new_int(config_struct.));  
  •     json_object_object_add(json_obj_A,"A_A1_Struct",json_obj_A_A1);  
  •   
  •     json_object_object_add(json_obj_B,"B_len",json_object_new_int(config_struct.B1.B_len));  
  •     json_object_object_add(json_obj_B,"B_len3",json_object_new_int(config_struct.B1.B_len3));  
  •   
  •     json_object_object_add(json_obj_C,"C_len",json_object_new_int(config_struct.C1.C_len));  
  •     json_object_object_add(json_obj_C,"C_len3",json_object_new_int(config_struct.C1.C_len3));  
  •   
  •     json_object_object_add(root_obj,"L1_A1",json_obj_A);  
  •     json_object_object_add(root_obj,"L1_B1",json_obj_B);  
  •     json_object_object_add(root_obj,"L1_C1",json_obj_C);  
  •       
  •     json_object_to_file(CONFIG_FILE,root_obj);  
  •     json_object_put(root_obj);  
  •     json_object_put(json_obj_A);  
  •     json_object_put(json_obj_A_A1);  
  •     json_object_put(json_obj_B);  
  •     json_object_put(json_obj_C);  
  •     json_object_put(new_obj);  
  •   
  •     return 0;  
  • }