ubuntu下安裝與使用json-c(原創)

Ubuntu 16.04 LTS 安裝json-c

 

json-c適用於開發人員使用c語言對json的編程。html

 

安裝json-c

1.經過git安裝,,json-c的github官網:

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

官網有詳細的安裝教程,這裏我挑出ubuntu的安裝例子。github

 

若以前你的ubuntu系統沒有安裝git工具,請先執行下面的命令,安裝git工具。編程

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

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

 

上述包安裝完成後,執行下面的命令,獲取json-c,執行sh腳本。ubuntu

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

 

執行sh後,編譯和安裝 json-capi

./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

原文出處:https://www.cnblogs.com/wybliw/p/10462708.html

相關文章
相關標籤/搜索