c 语言json-c实例,json-c 写配置文件实例

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

写配置文件:

#include

#include

#include

#include

#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的方式修改。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值