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