Linux C 编程——使用json-c库读取json格式的配置文件

 

1. 简介:

参考:

https://github.com/json-c/json-c/wiki

https://github.com/json-c/json-c

https://blog.csdn.net/u014530704/article/details/72674642?utm_source=copy

https://blog.csdn.net/w_ww_w/article/details/8295027

重点哈:https://blog.csdn.net/qimi123456/article/details/80426474

2. 编译运行:

json-c GitHub repo: https://github.com/json-c/json-c

$ git clone https://github.com/json-c/json-c.git
$ cd json-c
$ sh autogen.sh

followed by

$ ./configure  # --enable-threading
$ make
$ make install

To build and run the test programs:

$ make check
$ make USE_VALGRIND=0 check   # optionally skip using valgrind

3. 简单测试

1. 测试前,创建配置文件test.json,内容如下:

[root@localhost tests]# cat test.json
{"cmd":"SetPwd","params":{"id":111,"password":"test"}}

2. 编写测试文件test.c,内容如下:

#include 
#include 
#include 
#include 
#include "../json.h"

void main()
{
        /*从文件中获取json对象*/
        struct json_object * json_policy_array;
        json_policy_array = json_object_from_file("./test.json");
        printf("%s\n",json_object_to_json_string(json_policy_array));

        /*从json对象中获取json对象*/
        struct json_object * json_cmd ;
        json_cmd = json_object_object_get(json_policy_array,"cmd");
        printf("%s\n",json_object_to_json_string(json_cmd));

        /*从json对象中获取json对象*/
        struct json_object * json_params ;
        json_params = json_object_object_get(json_policy_array,"params");
        printf("%s\n",json_object_to_json_string(json_params));

        int id = -1;            //用于保存账号的值
        int pwd_len = 0;
        char *pwd = NULL;       //用于保存密码的值
        char *cmd = NULL;       //用于保存cmd的值

        /*从json对象中获取数据,首先获取对象,然后进行类型转换,转成int、float、string等*/
        id = json_object_get_int(json_object_object_get(json_params,"id"));
        printf("id = %d\n", id);
        cmd = json_object_get_string(json_object_object_get(json_policy_array,"cmd"));
        printf("cmd = %s\n", cmd);

        pwd_len = json_object_get_string_len(json_object_object_get(json_params,"password"));
        pwd = json_object_get_string(json_object_object_get(json_params,"password"));
        printf("pwd_len = %d\n", pwd_len);
        printf("pwd = %s\n", pwd);

        /*向json文件中添加数据*/
        //创建一个json对象newPobj
        char *result = "OK";
        //往json_policy_array里面添加键值对
        json_object_object_add(json_policy_array,"result",json_object_new_string(result));

        //创建一个json对象paramsProbj
        json_object  *paramsProbj = NULL;
        paramsProbj = json_object_new_object();
        //往paramsProbj里面添加键值对
        json_object_object_add(paramsProbj,"id",json_object_new_int(200));
        json_object_object_add(paramsProbj,"password",json_object_new_string("password2"));
        //把paramsProbj添加到newPobj对象中
        json_object_object_add(json_params,"paramsProbj",paramsProbj);

        //这时还是将配置信息存在内存中呢
        printf("%s\n",json_object_to_json_string(json_policy_array));

        //将内存中修改后的配置文件,写会到磁盘中
        json_object_to_file("./test.json",json_policy_array);

        //释放内存
        //引用计数方式,无需手动释放
}

3. 编译

[root@localhost tests]# gcc -ljson-c test.c
test.c: In function ‘main’:
test.c:32:6: warning: assignment discards ‘const’ qualifier from pointer target type [enabled by default]
  cmd = json_object_get_string(json_object_object_get(json_policy_array,"cmd"));
      ^
test.c:36:6: warning: assignment discards ‘const’ qualifier from pointer target type [enabled by default]
  pwd = json_object_get_string(json_object_object_get(json_params,"password")); 
      ^

4.运行

[root@localhost tests]# ./a.out 
{ "cmd": "SetPwd", "params": { "id": 111, "password": "test" } }
"SetPwd"
{ "id": 111, "password": "test" }
id = 111
cmd = SetPwd
pwd_len = 4
pwd = test
{ "cmd": "SetPwd", "params": { "id": 111, "password": "test", "paramsProbj": { "id": 200, "password": "password2" } }, "result": "OK" }

运行后,配置文件内容如下:

[root@localhost tests]# cat test.json 
{"cmd":"SetPwd","params":{"id":111,"password":"test","paramsProbj":{"id":200,"password":"password2"}},"result":"OK"}

4. 项目应用

在项目中,可以对此开源库进行封装,以使用项目本身;

为了是运行中的进程能够在配置文件改变后重新读取,需要引入信号机制:

1. 可以直接在信号处理函数中重新读取配置文件;

2. 也可以使用单独的线程,线程阻塞等待信号量,信号处理函数中释放信号量,线程获得信号量后,读写配置文件。

你可能感兴趣的:(Linux,C语言)