wybliw

Ubuntu 16.04 LTS 安装json-c

 

json-c适用于开发人员使用c语言对json的编程。

 

安装json-c

1.通过git安装,,json-c的github官网:

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

官网有详细的安装教程,这里我挑出ubuntu的安装例子。

 

若之前你的ubuntu系统没有安装git工具,请先执行下面的命令,安装git工具。

sudo apt install git
sudo apt install autoconf automake libtool
sudo apt install valgrind # optional

autoconf,automake,libtool是后面安装json-c要使用的包。

 

上述包安装完成后,执行下面的命令,获取json-c,执行sh脚本。

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

 

执行sh后,编译和安装 json-c

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

 

json-c安装完成后,执行下面的命令,编译运行test测试程序(To build and run the test programs)。

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

 

2.  通过Ubuntu 的 apt-get 安装

sudo apt-get install libjson0-dev libjson0

 

上述两种方法安装完成后,

在:

ls /usr/local/include/json/          #安装成功,出现json相关头文件
ls /usr/local/lib/                   #安装成功,出现json相关的库文件

 

编译使用json库的源文件时,需要指定头文件目录,JSON库所在目录,使用c99标准,告知程序使用的是哪个动态库。

如下:

gcc -o json-demo -g json-demo.c -std=c99 -I/usr/include/json -L/usr/local/lib/ -ljson

 

更改配置文件,指定库所在目录

vi /etc/ld.so.conf

在文件中加入   include /usr/local/lib/

 

json-c的使用

1.json-c的api介绍:

1 /*
2 该函数被弃用,请用 json_object_object_get_ex
3 */ 
4 
5 struct json_object* json_object_object_get(struct json_object* obj, const char *key)
1 /*
2 从obj实例中获取键key对应的json对象,并将找到的json对象指针存放到value中,
3 该函数不会改变引用计数
4 成功返回TRUE
5 失败返回FALSE
6 */ 
7 json_bool json_object_object_get_ex(struct json_object* obj, const char *key, struct json_object **value);
1 /*增加obj的引用计数*/
2 struct json_object* json_object_get(struct json_object *obj);
3 
4 /*减少obj的引用计数,当引用计数为0时,释放obj实例所占用的内存*/
5 int json_object_put(struct json_object *obj);
 1 /*
 2 检查obj是否是给定的类型 type是下列之一:
 3 json_type_null (i.e. obj == NULL),
 4 json_type_boolean,
 5 json_type_double,
 6 json_type_int,
 7 json_type_object,
 8 json_type_array,
 9 json_type_string,
10 */
11 
12 int json_object_is_type(struct json_object *obj, enum json_type type);
1 /*
2 获取obj类型
3 */
4 enum json_type json_object_get_type(struct json_object *obj);
1 /*
2 将obj实例转化成一个json格式的字符串
3 等同于json_object_to_json_string_ext(obj, JSON_C_TO_STRING_SPACED)
4 返回一个json格式的字符串指针
5 */ 
6 
7 const char* json_object_to_json_string(struct json_object *obj);

 

参考博客有:

json-c接口

https://blog.csdn.net/u012819339/article/details/51733323

https://www.cnblogs.com/qingergege/p/5997762.html

 

//end

 

posted on 2019-03-02 20:12 wybliw 阅读(...) 评论(...) 编辑 收藏

相关文章: