0、JSON簡介
JSON(JavaScript Object Notation) 是一種輕量級的數據交換格式。易於人閱讀和編寫。同時也易於機器解析和生成。
JSON採用完全獨立於語言的文本格式,但是也使用了類似於C語言家族的習慣(包括C, C++, C#, Java, JavaScript, Perl, Python等)。
這些特性使JSON成爲理想的數據交換語言。 跟XML相比,JSON的優勢在於格式簡潔短小,特別是在處理大量複雜數據的時候,這個優勢便顯得非常突出。
從各瀏覽器的支持來看,JSON解決了因不同瀏覽器對XML DOM解析方式不同而引起的問題。
★JASON的學習資料
http://www.json.org/ (英文) http://www.json.org/json-zh.html (中文)
http://www.w3school.com.cn/json/
http://www.ibm.com/developerworks/cn/web/wa-lo-json/
json標準 https://tools.ietf.org/html/rfc7159
★JASON-C的簡介
JSON庫多種多樣,但是JSON-C由於兼容性好,支持UTF-8,所以使用比較廣泛。
就json來說,由於結構比較簡單,不用庫也是可以的。
但json-c提供了超出json範圍的一些功能,實際上完成了數據序列化和反序列化,數據的存儲和檢索,數據對象化等功能。還是非常有使用價值的。
https://github.com/json-c/json-c/wiki
官方API手冊 : http://json-c.github.io/json-c/json-c-0.13.1/doc/html/index.html
http://zengriguang.blog.163.com/blog/static/17076248720121080187635/
1、下載安裝JSON-C
1.獲取源碼
[root]# yum install git
[root]# git clone https://github.com/json-c/json-c.git
2.編譯安裝
[root]# cd json-c
[root]# ./autogen.sh
[root]# ./configure
[root]# make
[root]# make install
3.查看是否存在庫文件
修改ldconfig配置文件,將路徑添加進去
vi /etc/ld.so.conf.d/fakeroot-x86_64-linux-gnu.conf
/usr/local/lib #如果重名,這個需要放在前面,否則不會生效
/usr/lib/x86_64-linux-gnu/libfakeroot
[root]# ldconfig -p|grep json-c
libjson-c.so.4 (libc6,x86-64) => /usr/local/lib/libjson-c.so.4
libjson-c.so.2 (libc6,x86-64) => /lib/x86_64-linux-gnu/libjson-c.so.2
libjson-c.so (libc6,x86-64) => /usr/local/lib/libjson-c.so
說明安裝成功
2、示例
json-c_demo.c
#include <stdio.h>
#include <json.h>
void test_jsonc()
{
struct json_object *infor_object = NULL;
infor_object = json_object_new_object();
if (NULL == infor_object)
{
printf("new json object failed.\n");
return;
}
struct json_object *para_object = NULL;
para_object = json_object_new_object();
if (NULL == para_object)
{
json_object_put(infor_object);//free
printf("new json object failed.\n");
return;
}
struct json_object *array_object = NULL;
array_object = json_object_new_array();
if (NULL == array_object)
{
json_object_put(infor_object);//free
json_object_put(para_object);//free
printf("new json object failed.\n");
return;
}
/*添加json值類型到數組中*/
json_object_array_add(array_object, json_object_new_int(256));
json_object_array_add(array_object, json_object_new_int(257));
json_object_array_add(array_object, json_object_new_int(258));
json_object_object_add(para_object, "DeviceId", json_object_new_string("sn_iso_9000"));
json_object_object_add(para_object, "MacAddr", json_object_new_string("AA:BB:CC:DD:EE:FF"));
json_object_object_add(para_object, "Visible", json_object_new_int(1));
/*添加json名稱和值到json對象集合中*/
json_object_object_add(infor_object, "method", json_object_new_string("GetSystemInfo"));
json_object_object_add(infor_object, "param", para_object);
json_object_object_add(infor_object, "id", json_object_new_string("101"));
/*添加數組集合到json對象中*/
json_object_object_add(infor_object, "array", array_object);
printf("-----------json infor ---------------------------\n");
printf("%s\n", json_object_to_json_string(infor_object));
printf("-----------json infor ---------------------------\n");
struct json_object *result_object = NULL;
result_object = json_object_object_get(infor_object, "method");
printf("-----------result_object method ---------------------------\n");
printf("%s\n", json_object_to_json_string(result_object));
printf("-----------result_object method---------------------------\n");
result_object = json_object_object_get(infor_object, "param");
printf("-----------result_object param ---------------------------\n");
printf("%s\n", json_object_to_json_string(result_object));
printf("-----------result_object param---------------------------\n");
result_object = json_object_object_get(infor_object, "array");
printf("-----------result_object array---------------------------\n");
printf("%s\n", json_object_to_json_string(result_object));
printf("-----------result_object array---------------------------\n");
int i;
for(i = 0; i < json_object_array_length(result_object); i++) {
struct json_object *obj = json_object_array_get_idx(result_object, i);
printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));
}
json_object_put(infor_object);//free
}
int main(int argc, char *argv[])
{
test_jsonc();
return 0;
}
編譯:
1)指定運行路徑 -Wl,-rpath=/usr/local/lib/
gcc -o json-c_demo json-c_demo.c -L/usr/local/lib/ -ljson-c -I/usr/local/include/json-c -Wl,-rpath=/usr/local/lib/
2)將json-c庫放在ldconfig環境變量裏(見步驟3)
3)設置一個全局變量
export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH
運行結果:
-----------json infor ---------------------------
{ "method": "GetSystemInfo", "param": { "DeviceId": "sn_iso_9000", "MacAddr": "AA:BB:CC:DD:EE:FF", "Visible": 1 }, "id": "101", "array": [ 256, 257, 258 ] }
-----------json infor ---------------------------
-----------result_object method ---------------------------
"GetSystemInfo"
-----------result_object method---------------------------
-----------result_object param ---------------------------
{ "DeviceId": "sn_iso_9000", "MacAddr": "AA:BB:CC:DD:EE:FF", "Visible": 1 }
-----------result_object param---------------------------
-----------result_object array---------------------------
[ 256, 257, 258 ]
-----------result_object array---------------------------
[0]=256
[1]=257
[2]=258