什么是JSON
JSON是一種輕量級的數據交換格式,可讀性強、編寫簡單。鍵值對組合編寫規則,鍵名使用雙引號包裹,冒號:分隔符后面緊跟著數值,有兩種常用的數據類型是對象和數組。
對象:使用花括號{}包裹起來的內容,數據結構{“key1”: “value1”, “key2”:“value2” …},key為對象的屬性,value為對象的值。
數值:使用中括號[]包裹起來的內容,數據結構{“key”: [“value1”, “value2”, “value3” …]}。
?
CentOS 7 安裝cJSON 庫
cJSON Github 地址:https://github.com/DaveGamble/cJSON
步驟1:首先,你需要下載cJSON的源代碼。你可以從https://github.com/DaveGamble/cJSON或者源代碼官方網站下載。并上傳至/usr/local/source_code/??
步驟2:下載完成后,需要將源代碼解壓,可以使用以下命令:?
[root@localhost source_code]# tar -zxvf cJSON-1.7.15.tar.gz
?步驟3:解壓后,切換到源代碼目錄:?
[root@localhost source_code]# cd cJSON-1.7.15
步驟4:生成cJSON動態/靜態庫,執行如下指令:?
[root@localhost cJSON-1.7.15]# mkdir build
[root@localhost cJSON-1.7.15]# cd build/
[root@localhost build]# cmake ..
CMake Deprecation Warning at CMakeLists.txt:2 (cmake_minimum_required):Compatibility with CMake < 2.8.12 will be removed from a future version ofCMake.Update the VERSION argument <min> value or use a ...<max> suffix to tellCMake that the project does not need compatibility with older versions.-- The C compiler identification is GNU 8.3.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
******
[root@localhost build]# make && make install
[ 2%] Building C object CMakeFiles/cjson.dir/cJSON.c.o
[ 4%] Linking C shared library libcjson.so
[ 4%] Built target cjson
[ 6%] Building C object CMakeFiles/cJSON_test.dir/test.c.o
[ 8%] Linking C executable cJSON_test
[ 8%] Built target cJSON_test
[ 11%] Building C object tests/CMakeFiles/unity.dir/unity/src/unity.c.o
[ 13%] Linking C static library libunity.a
[ 13%] Built target unity
[ 15%] Building C object tests/CMakeFiles/print_number.dir/print_number.c.o
[ 17%] Linking C executable print_number
-- 省略--
[100%] Built target fuzz_main
Install the project...
-- Install configuration: ""
-- Installing: /usr/local/include/cjson/cJSON.h
-- Installing: /usr/local/lib64/pkgconfig/libcjson.pc
-- Installing: /usr/local/lib64/libcjson.so.1.7.15
-- Installing: /usr/local/lib64/libcjson.so.1
-- Installing: /usr/local/lib64/libcjson.so
-- Installing: /usr/local/lib64/cmake/cJSON/cjson.cmake
-- Installing: /usr/local/lib64/cmake/cJSON/cjson-noconfig.cmake
-- Installing: /usr/local/lib64/cmake/cJSON/cJSONConfig.cmake
-- Installing: /usr/local/lib64/cmake/cJSON/cJSONConfigVersion.cmake
遇到的問題及解決辦法
編譯執行./test_cjson,提示如下截圖錯誤信息:
[root@localhost cJSON_demo]# ./test_cjson
./test_cjson: error while loading shared libraries: libcjson.so.1: cannot open shared object file: No such file or directory
從報錯的原因上看,本機上沒有找到cJOSN類庫的靜態庫/動態庫libcjson.so。
首先檢查/usr/local/lib 和/usr/local/include 目錄中是否包含cjson 靜態或動態庫,可以執行如下指令:
ls /usr/local/lib | grep cjson
ls /usr/local/include | grep cjson
從上面分組 查詢結果可知:cjson 沒有在/usr/local/lib 庫中生成cjson 靜態/動態庫鏈接。
再次查看cJOSN 在執行make && make install 指令時,對應cJSON 靜態/動態庫生成鏈接存放目錄地址。
從上述截圖可知cJSON 靜態/動態庫被安裝到了/usr/local/lib64 目錄中。
將/usr/local/lib64 目錄添加至本機靜態/動態庫鏈接目錄文件中,執行如下指令:
[root@localhost cJSON_demo]# cat /etc/ld.so.conf.d/usr-libs.conf
/usr/local/lib
[root@localhost cJSON_demo]# vi /etc/ld.so.conf.d/usr-libs.conf
[root@localhost ~]# cat /etc/ld.so.conf.d/usr-libs.conf
/usr/local/lib
/usr/local/lib64
[root@localhost cJSON_demo]# sudo ldconfig
再次編譯執行./test_cjson, Main 函數正確輸出。
cJSON快速入門
在/usr/local/source_code 新增?cJSON_demo目錄并新增test_cjson.c?文件,文件內容如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cjson/cJSON.h>int main()
{cJSON *json = NULL;cJSON *node = NULL;cJSON *tnode = NULL;cJSON *tnode2 = NULL;char *json_data = NULL;int i, j, size, size2;char *data = "{\"serialNumber\":\"212089842348362300\", \\"cellularInfo\":\[\{\"name\":\"ethernet0/0/1\",\\"switch\":\"0\"},\{\"name\":\"ethernet0/0/2\",\\"switch\":\"1\"},\{\"name\":\"ethernet0/0/3\",\\"switch\":\"0\"}\],\\"family\":[\"father\",\"mother\",\"brother\",\"sister\",\"somebody\"]\}";json = cJSON_Parse(data);json_data = cJSON_Print(json);printf("data: %s\n", json_data);free(json_data);node = cJSON_GetObjectItem(json,"serialNumber");if(node == NULL)printf("serialNumber: no\n");elseprintf("serialNumber: ok\n");node = cJSON_GetObjectItem(json, "family");if (node == NULL)printf("family: no\n");elseprintf("family: ok\n");if (node->type == cJSON_Array){printf("family array size is %d\n", cJSON_GetArraySize(node));size = cJSON_GetArraySize(node);for (i=0; i<size; i++){tnode = cJSON_GetArrayItem(node, i);if (tnode->type == cJSON_String)printf("%d: %s\n", i, tnode->valuestring);elseprintf("node type is not string, value = %d\n", tnode->type);}}node = cJSON_GetObjectItem(json, "cellularInfo");if(node == NULL)printf("cellularInfo: no\n");elseprintf("cellularInfo: ok\n");if (node->type == cJSON_Array){printf("cellularInfo array size is %d\n", cJSON_GetArraySize(node));size = cJSON_GetArraySize(node);for (i=0; i<size; i++){tnode = cJSON_GetArrayItem(node, i);if (tnode->type == cJSON_String)printf("%d: %s\n", i, tnode->valuestring);else if (tnode->type == cJSON_Object){size2 = cJSON_GetArraySize(tnode);for (j=0; j<size2; j++){tnode2 = cJSON_GetArrayItem(tnode, j);if (tnode2->type == cJSON_String)printf("%d-%d: %s\n", i, j, tnode2->valuestring);elseprintf("tnod2 type is err\n");}}elseprintf("node type is not string, value = %d\n", tnode->type);}}cJSON_Delete(json);return 0;
}
編譯源碼并執行:
[root@localhost cJSON_demo]# gcc -o test_cjson test_cjson.c -lcjson
[root@localhost cJSON_demo]# ./test_cjson
data: {"serialNumber": "212089842348362300","cellularInfo": [{"name": "ethernet0/0/1","switch": "0"}, {"name": "ethernet0/0/2","switch": "1"}, {"name": "ethernet0/0/3","switch": "0"}],"family": ["father", "mother", "brother", "sister", "somebody"]
}
serialNumber: ok
family: ok
family array size is 5
0: father
1: mother
2: brother
3: sister
4: somebody
cellularInfo: ok
cellularInfo array size is 3
0-0: ethernet0/0/1
0-1: 0
1-0: ethernet0/0/2
1-1: 1
2-0: ethernet0/0/3
2-1: 0
cJSON 參考資料
cJSON Github 官網地址:https://github.com/DaveGamble/cJSON
cJSON 開發參考:?https://zhuanlan.zhihu.com/p/55095477?utm_oi=892471685738024960