1. 程式人生 > >【C程式】JSON庫怎麼用,json.h全解

【C程式】JSON庫怎麼用,json.h全解

標頭檔案:#include "json/json.h"    // 當前目錄下使用:char *str = "{\"abc\": 123, \"wds\": 12.3, \"qwe\": \"ddd\", \"bool0\": false, \"bool1\": true, \"arr\";編譯註意:需要加上 -ljson 庫。1.宣告json物件struct json_object *my_obj;2.將符合json格式的字串構造為一個json物件my_obj = json_tokener_parse(str);printf("1-tokener parse: %s\n", json_object_to_json_string(my_obj));3.在物件裡用key值去找,然後刪除它的key:valuejson_object_object_del
(my_obj, "abc");printf("2-tokener parse: %s\n", json_object_to_json_string(my_obj));4.在物件裡用key值去找,獲取它的value值struct json_object *ret_obj; ret_obj = json_object_object_get(my_obj, "wds"); // key == "wds"printf("obj_get-wds:%s\n", json_object_to_json_string(ret_obj));5.在json_object_object_get引用一次,將json物件的引用計數減1,子物件引用計數為0時釋放所佔用記憶體json_object_put
(ret_obj);6.獲取不同的json資料型別,int.double.string.array.objectstruct json_object *val = json_object_new_int(18542);7.將key:value加入到物件my_obj中json_object_object_add(my_obj, "wds", val);   printf("3-object add: %s\n", json_object_to_json_string(my_obj));json_object_put(val);     // 參考第5.8.輸出結果printf("get string:%s\n", json_object_get_string
(my_obj)); printf("get lh:%d\n", json_object_get_object(my_obj)); printf("get int:%d\n", json_object_get_int(my_obj));// 這個介面函式,包含很多資料型別(array, int, string, object)組成json包,socket傳輸,為上層提供資料,便於展示。 make_a_common_json_string();json_object_put(my_obj);     // 參考第5. 9.介面函式void make_a_common_json_string() {     printf("####################################\n");     int i=0;     struct json_object *obj_all  = json_object_new_object();     struct json_object *obj_info = json_object_new_object();     struct json_object *obj_work = json_object_new_object();     struct json_object *obj_life = json_object_new_object();     struct json_object *arr_month_grade = json_object_new_array();     for(i=1; i<13; i++){         struct json_object *obj_int = json_object_new_int(i);         json_object_array_add(arr_month_grade, obj_int);         }     json_object_object_add(obj_work, "position", json_object_new_string("programmer"));     json_object_object_add(obj_work, "department", json_object_new_string("make code"));     json_object_object_add(obj_work, "address", json_object_new_string("BJ.3.103"));     json_object_object_add(obj_work, "mounth_grade", arr_month_grade);     json_object_object_add(obj_info, "Work", obj_work);     json_object_object_add(obj_life, "Name", json_object_new_string("DS.wen"));     json_object_object_add(obj_life, "Age", json_object_new_int(24.34));     json_object_object_add(obj_life, "M-O", json_object_new_string("Male"));     json_object_object_add(obj_info, "Life", obj_life);     json_object_object_add(obj_all,"W.DS_Info", obj_info);     printf("all_string:%s\n", json_object_to_json_string(obj_all));     json_object_put(obj_all);     return ; } >>輸出結果:/*************************************列印結果********************************************/ 1-tokener parse: { "abc": 123, "wds": 12.300000, "qwe": "ddd", "bool0": false, "bool1": true, "arr": [ 12, 3, 0, 5, null ] } 2-tokener parse: { "wds": 12.300000, "qwe": "ddd", "bool0": false, "bool1": true, "arr": [ 12, 3, 0, 5, null ] } obj_get-wds:12.300000 3-object add: { "wds": 18542, "qwe": "ddd", "bool0": false, "bool1": true, "arr": [ 12, 3, 0, 5, null ] } get string:{ "wds": 18542, "qwe": "ddd", "bool0": false, "bool1": true, "arr": [ 12, 3, 0, 5, null ] } get lh:26383664 get int:0 #################################### all_string:{ "W.DS_Info": { "Work": { "position": "programmer", "department": "make code", "address": "BJ.3.103", "mounth_grade": [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] }, "Life": { "Name": "DS.wen", "Age": 24, "M-O": "Male" } } } /***************************************************************************************/ JSON物件的生成Json物件的型別:json_type_object, “名稱/值”對的集合Json物件的值型別              json_type_boolean,json_type_double,json_type_int,json_type_array, “值”的集合json_type_stringstructjson_object *json_object_new_object();說明:建立個空的json_type_object型別JSON物件。struct json_object *json_object_new_boolean(boolean b);說明:       建立個json_type_boolean值型別json物件boolean json_object_get_boolean(structjson_object *obj);說明:       從json物件中boolean值型別得到boolean值同樣:       structjson_object* json_object_new_int(int i)       intjson_object_get_int(struct json_object *this)       structjson_object* json_object_new_double(double d)       doublejson_object_get_double(struct json_object *this)       structjson_object* json_object_new_string(char *s)       char*json_object_get_string(struct json_object *this)structjson_object *json_object_new_array();說明:建立個空的json_type_array型別JSON陣列值物件。structjson_object *json_tokener_parse(char *str);說明:由str裡的JSON字串生成JSON物件,str是son_object_to_json_string() 生成的。引數:    str – json字串structjson_object *json_object_object_get(struct json_object * json,char *name);說明:從json中按名字取一個物件。引數:    json – json物件    name -  json域名字3.2 JSON物件的釋放struct json_object * *json_object_get(struct json_object * this)說明:增加物件引用計數。使用c庫最關心的是記憶體誰來分配, 誰來釋放. jsonc的記憶體管理方式, 是基於引用計數的記憶體樹(鏈), 如果把一個struct json_object 物件a, add到另一個物件b上, 就不用顯式的釋放(json_object_put) a了, 相當於把a掛到了b的物件樹上, 釋放b的時候, 就會釋放a. 當a即add到b上, 又add到物件c上時會導致a被釋放兩次(double free), 這時可以增加a的引用計數(呼叫函式json_object_get(a)), 這時如果先釋放b, 後釋放c, 當釋放b時, 並不會真正的釋放a, 而是減少a的引用計數為1, 然後釋放c時, 才真正釋放a.引數:       this – json物件Void json_object_put(struct json_object *this)說明:       減少物件引用次數一次,當減少到0就釋放(free)資源引數:       this – json物件樣例片段:  my_string =json_object_new_string("\t");/*輸出 my_string=  */ \t(table鍵)  printf("my_string=%s\n",json_object_get_string(my_string));/*轉換json格式字串輸出my_string.to_string()="\t"*/printf("my_string.to_string()=%s\n",json_object_to_json_string(my_string));/*釋放資源*/  json_object_put(my_string);3.3 JSON物件的操作Int json_object_is_type(structjson_object * this, enum json_type type)說明:       檢查json_object是json的某個型別引數:       this:json_object 例項       type:json_type_boolean,json_type_double, json_type_int, json_type_object,json_type_array, json_type_stringenum json_type json_object_get_type(struct json_object * this )說明:       得到json_object的型別。引數:       this – json物件char * json_object_to_json_string(structjson_object * this)說明:       將json_object內容轉換json格式字串,其中可能含有轉義符。引數:       this – json物件返回值:       Json格式字串void json_object_object_add(structjson_object* obj, char *key, struct json_object *val);說明:       添加個物件域到json物件中引數:       Obj – json物件       key – 域名字       val – json值物件void json_object_object_del(structjson_object* obj, char *key);說明:       刪除key值json物件引數:       ob j – json物件       key – 域名字int json_object_array_length(structjson_object *obj);說明:       得到json物件陣列的長度。引數:       ob j – json陣列值物件extern int json_object_array_add(struct json_object*obj,                             struct json_object *val);說明:       新增一元素在json物件陣列末端引數:ob j – json陣列值物件val – json值物件*int json_object_array_put_idx(structjson_object *obj, int idx,                                 struct json_object *val);說明:       在指定的json物件陣列下標插入或替換一個json物件元素。引數:ob j – json陣列值物件val – json值物件  idx– 陣列下標struct json_object *json_object_array_get_idx(struct json_object * json_array,int i);說明:從陣列中,按下標取JSON值物件。引數:       json_array– json 陣列型別物件       i– 陣列下標位置定義巨集 json_object_object_foreach(obj,key,val)說明:       遍歷json物件的key和值 (key, val預設引數不變)樣例片段:  /*建立個空json物件值陣列型別*/  my_array = json_object_new_array();  /*新增json值型別到陣列中*/  json_object_array_add(my_array,json_object_new_int(1));  json_object_array_add(my_array,json_object_new_int(2));  json_object_array_add(my_array, json_object_new_int(3));  json_object_array_put_idx(my_array, 4,json_object_new_int(5));  printf("my_array=\n");  for(i=0; i <json_object_array_length(my_array); i++) {    struct json_object *obj =json_object_array_get_idx(my_array, i);    printf("\t[%d]=%s\n", i,json_object_to_json_string(obj));  }  printf("my_array.to_string()=%s\n",json_object_to_json_string(my_array));  my_object = json_object_new_object();  /*新增json名稱和值到json物件集合中*/  json_object_object_add(my_object,"abc", json_object_new_int(12));  json_object_object_add(my_object,"foo", json_object_new_string("bar"));  json_object_object_add(my_object,"bool0", json_object_new_boolean(0));  json_object_object_add(my_object,"bool1", json_object_new_boolean(1));  json_object_object_add(my_object,"baz", json_object_new_string("bang"));  /*同樣的key 新增會替換掉*/  json_object_object_add(my_object,"baz", json_object_new_string("fark"));  json_object_object_del(my_object,"baz");  /*新增陣列集合到json物件中*/json_object_object_add(my_object, "arr",my_array);  printf("my_object=\n");  /*遍歷json物件集合*/  json_object_object_foreach(my_object, key,val) {    printf("\t%s: %s\n", key,json_object_to_json_string(val));  }  json_object_put(my_object);4.      JSON例項開發4.1 樣例1#include <stdio.h>#include <stdlib.h>#include <stddef.h>#include <string.h>#include "json.h"int main(int argc, char **argv){ struct json_tokener *tok; struct json_object *my_string, *my_int, *my_object, *my_array; struct json_object *new_obj;  inti; my_string = json_object_new_string("\t");/*輸出 my_string=   */ printf("my_string=%s\n", json_object_get_string(my_string));/*轉換json格式字串 輸出my_string.to_string()="\t"*/printf("my_string.to_string()=%s\n",json_object_to_json_string(my_string));/*釋放資源*/ json_object_put(my_string); my_string = json_object_new_string("\\"); printf("my_string=%s\n", json_object_get_string(my_string)); printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); json_object_put(my_string); my_string = json_object_new_string("foo"); printf("my_string=%s\n", json_object_get_string(my_string)); printf("my_string.to_string()=%s\n",json_object_to_json_string(my_string)); my_int = json_object_new_int(9); printf("my_int=%d\n", json_object_get_int(my_int)); printf("my_int.to_string()=%s\n",json_object_to_json_string(my_int));  /*建立個空json物件值陣列型別*/ my_array = json_object_new_array();  /*新增json值型別到陣列中*/ json_object_array_add(my_array, json_object_new_int(1)); json_object_array_add(my_array, json_object_new_int(2)); json_object_array_add(my_array, json_object_new_int(3)); json_object_array_put_idx(my_array, 4, json_object_new_int(5)); printf("my_array=\n"); for(i=0; i < json_object_array_length(my_array); i++) {   struct json_object *obj = json_object_array_get_idx(my_array, i);   printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));  } printf("my_array.to_string()=%s\n",json_object_to_json_string(my_array)); my_object = json_object_new_object();  /*新增json名稱和值到json物件集合中*/ json_object_object_add(my_object, "abc",json_object_new_int(12)); json_object_object_add(my_object, "foo",json_object_new_string("bar")); json_object_object_add(my_object, "bool0",json_object_new_boolean(0)); json_object_object_add(my_object, "bool1",json_object_new_boolean(1)); json_object_object_add(my_object, "baz",json_object_new_string("bang"));  /*同樣的key 新增會替換掉*/ json_object_object_add(my_object, "baz",json_object_new_string("fark")); json_object_object_del(my_object, "baz"); printf("my_object=\n");  /*遍歷json物件集合*/ json_object_object_foreach(my_object, key, val) {   printf("\t%s: %s\n", key, json_object_to_json_string(val));  } printf("my_object.to_string()=%s\n",json_object_to_json_string(my_object)); /*對些不規則的串做了些解析測試*/ new_obj = json_tokener_parse("\"\003\""); printf("new_obj.to_string()=%s\n",json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("/* hello */\"foo\""); printf("new_obj.to_string()=%s\n",json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("// hello\n\"foo\""); printf("new_obj.to_string()=%s\n",json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj =json_tokener_parse("\"\\u0041\\u0042\\u0043\""); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("null"); printf("new_obj.to_string()=%s\n",json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("True"); printf("new_obj.to_string()=%s\n",json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("12"); printf("new_obj.to_string()=%s\n",json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("12.3");  /*得到json double型別 printf("new_obj.to_string()=%s\n",json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("[\"\\n\"]"); printf("new_obj.to_string()=%s\n",json_object_to_json_string(new_obj));  json_object_put(new_obj); new_obj = json_tokener_parse("[\"\\nabc\\n\"]"); printf("new_obj.to_string()=%s\n",json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("[null]"); printf("new_obj.to_string()=%s\n",json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("[]"); printf("new_obj.to_string()=%s\n",json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("[false]");  printf("new_obj.to_string()=%s\n",json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj =json_tokener_parse("[\"abc\",null,\"def\",12]"); printf("new_obj.to_string()=%s\n",json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("{}"); printf("new_obj.to_string()=%s\n",json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("{ \"foo\":\"bar\" }"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("{ \"foo\":\"bar\", \"baz\": null, \"bool0\": true }"); printf("new_obj.to_string()=%s\n",json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("{ \"foo\": [null,\"foo\"] }"); printf("new_obj.to_string()=%s\n",json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("{ \"abc\": 12,\"foo\": \"bar\", \"bool0\": false,\"bool1\": true, \"arr\": [ 1, 2, 3, null, 5 ] }"); printf("new_obj.to_string()=%s\n",json_object_to_json_string(new_obj)); json_object_put(new_obj); new_obj = json_tokener_parse("{ foo }"); if(is_error(new_obj)) printf("got error as expected\n"); new_obj = json_tokener_parse("foo"); if(is_error(new_obj)) printf("got error as expected\n"); new_obj = json_tokener_parse("{ \"foo"); if(is_error(new_obj)) printf("got error as expected\n");  /*test incremental parsing */  tok= json_tokener_new();  new_obj = json_tokener_parse_ex(tok, "{\"foo", 6); if(is_error(new_obj)) printf("got error as expected\n"); new_obj = json_tokener_parse_ex(tok, "\": {\"bar",8); if(is_error(new_obj)) printf("got error as expected\n"); new_obj = json_tokener_parse_ex(tok, "\":13}}", 6); printf("new_obj.to_string()=%s\n",json_object_to_json_string(new_obj)); json_object_put(new_obj); json_tokener_free(tok); json_object_put(my_string); json_object_put(my_int); json_object_put(my_object); json_object_put(my_array);/*如果前面沒有新增到物件中,必須顯示釋放, 如果新增到物件中,已經釋放物件,則無需呼叫, 在這務必小心,否則很容易記憶體洩漏*/ return 0;}輸出結果:my_string=my_string.to_string()="\t"my_string=\my_string.to_string()="\\"my_string=foomy_string.to_string()="foo"my_int=9my_int.to_string()=9my_array=       [0]=1       [1]=2       [2]=3       [3]=null       [4]=5my_array.to_string()=[ 1, 2, 3, null, 5 ]my_object=       abc: 12       foo: "bar"       bool0: false       bool1: truemy_object.to_string()={ "abc":12, "foo": "bar", "bool0": false,"bool1": true }new_obj.to_string()="\u0003"new_obj.to_string()="foo"new_obj.to_string()="foo"new_obj.to_string()="ABC"new_obj.to_string()=nullnew_obj.to_string()=truenew_obj.to_string()=12new_obj.to_string()=12.300000new_obj.to_string()=[ "\n" ]new_obj.to_string()=[ "\nabc\n" ]new_obj.to_string()=[ null ]new_obj.to_string()=[ ]new_obj.to_string()=[ false ]new_obj.to_string()=[ "abc",null, "def", 12 ]new_obj.to_string()={ }new_obj.to_string()={ "foo":"bar" }new_obj.to_string()={ "foo":"bar", "baz": null, "bool0": true }new_obj.to_string()={ "foo": [null, "foo" ] }new_obj.to_string()={ "abc": 12,"foo": "bar", "bool0": false, "bool1":true, "arr": [ 1, 2, 3, null, 5 ] }got error as expectedgot error as expectedgot error as expectednew_obj.to_string()={ "foo": {"bar": 13 } }備註Json-c-0.8版本對0.7版本做的更新:* 新增va_end 給指標至NULL 增加程式健壯性* 新增巨集使得能夠編譯出調試程式碼* 解決個bug 在指數中使用大寫字母E  * 新增 stddef.h 標頭檔案* 允許編譯json-c 使用-Werror