ubuntu下安装与使用json-c(原创)

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 该函数被弃用,请用 json_object_object_get_ex 
 2 
 3 struct json_object* json_object_object_get(struct json_object* obj, const char *key)
 4 
 5 
 6 从obj实例中获取键key对应的json对象,并将找到的json对象指针存放到value中,
 7 该函数不会改变引用计数
 8 成功返回TRUE
 9 失败返回FALSE
10 
11 json_bool json_object_object_get_ex(struct json_object* obj, const char *key, struct json_object **value);

参考博客有:

json-c接口

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

json-c开发指南

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

//end

猜你喜欢

转载自www.cnblogs.com/wybliw/p/10462708.html
今日推荐