jsonc 写配置文件比较简单,并且解析配置文件也比较省事。
写配置文件:
- #include <stdio.h>
- #include <stdlib.h>
- #include <stddef.h>
- #include <string.h>
- #include "./inc/json.h"
- #define CONFIG_FILE "config.json"
- 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.A1.name));
- json_object_object_add(json_obj_A,"A_len",json_object_new_int(config_struct.A1.len));
- 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;
- }
- 写完的配置文件如下:
- {
- "L1_root1": 1,
- "L1_root2": 2,
- "L1_root_token": "token1",
- "L1_A1": {
- "A_name": "A_name_1",
- "A_len": 101,
- "A_A1_Struct": {
- "A_A1_len3": 1001,
- "A_A1_len4": 1002
- }
- },
- "L1_B1": {
- "B_len": 201,
- "B_len3": 202
- },
- "L1_C1": {
- "C_len": 301,
- "C_len3": 302
- }
- }
符合期望的东西。
json_object_object_add 该动作如果要添加的Key已经存在,则修改之,否则才会添加。
所以想要修改配置文件,直接用add的方法就好了,不要用del+add的方式修改。