cjson構建 cJSON的構造和解析

2021-10-12 15:57:45 字數 4365 閱讀 8527

對於cjson的使用,我主要是用來模擬遠端伺服器端返回的乙個json型別的目錄結構,客戶端進行獲取並進行解析,把解析出來的目錄按照原本的結構顯示在本地。

cjson是乙個超輕巧,攜帶方便,單檔案,簡單的可以作為ansi-c標準的json解析器。

cjson結構體:

typedef

struct cjson ;

root=cjson_createobject();

cjson_additemtoobject(root, "root",

img=cjson_createobject());

cjson_addnumbertoobject(img,"key",800);

cjson_addnumbertoobject(img,"value",600);

cjson_addstringtoobject(img,"title","sugon");

cjson_additemtoobject(img,"child",thm=cjson_createobject());

cjson_addnumbertoobject(thm,"key",125);

cjson_addstringtoobject(thm,"value","100");

cjson_addstringtoobject(thm,"url","www.sugon.com");

cjson_additemtoobject(img,"nums",

cjson_createintarray(nums,4));

out=cjson_print(root);

cjson_delete(root);

return out;

char

create4()

cjson *root,*dir1,*dir2;

char *out;

const char *ro = "root";

root=cjson_createobject();

cjson_additemtoobject(root,ro,dir1=cjson_createarray());

cjson_addnumbertoobject(dir1,"key",800);

cjson_addnumbertoobject(dir1,"value",600);

cjson_addstringtoobject(dir1,"title","key and

value");

cjson_additemtoobject(root,ro,dir2=cjson_createarray());

cjson_addnumbertoobject(dir2,"value",125);

cjson_addstringtoobject(dir2,"key","100");

cjson_addstringtoobject(dir2,"title","value

and key");

out=cjson_print(root);

cjson_delete(root);

return out;

void

parse1(char

*out)

cjson *

root,*arrayitem,*item,*name,*path,*flag;

int i = 0,size = 0;

char *pr = null,*na = null,*pa =

null,*fl = null;

//將字串解析成json結構體

root = cjson_parse(out);

//根據結構體獲取陣列大小

size = cjson_getarraysize(root);

//printf("%d\n",size);

//遍歷陣列

for(i=0;i

//獲取第i個陣列項

arrayitem = cjson_getarrayitem(root,i);

if(arrayitem)

//printf("%s\n","start......");

//講json結構體轉換成字串

pr = cjson_print(arrayitem);

item = cjson_parse(pr);

name = cjson_getobjectitem(item,"name");

path = cjson_getobjectitem(item,"path");

flag = cjson_getobjectitem(item,"flag");

na = cjson_print(name);

pa = cjson_print(path);

fl = cjson_print(flag);

//printf("%s\n",pr);

printf("name:%s\n",na);

printf("path:%s\n",pa);

printf("flag:%s\n\n",fl);

void

parse2(char

*out)

cjson *

root,*root,*child,*arrayitem,*item,*name,*path,*flag;

int i = 0,size = 0;

char *pr = null,*na = null,*pa =

null,*fl = null;

root = cjson_parse(out);

if(root)

root = cjson_getobjectitem(root,"root");

if(root)

name = cjson_getobjectitem(root,"name");

path = cjson_getobjectitem(root,"path");

flag = cjson_getobjectitem(root,"flag");

na = cjson_print(name);

pa = cjson_print(path);

fl = cjson_print(flag);

printf("root:\n");

printf("name:%s\n",na);

printf("path:%s\n",pa);

printf("flag:%s\n\n",fl);

child = cjson_getobjectitem(root,"child");

if(child)

size = cjson_getarraysize(child);

//printf("%d\n",size);

printf("child:\n");

for(i=0;i

arrayitem = cjson_getarrayitem(child,i);

if(arrayitem)

//printf("%s\n","start......");

pr = cjson_print(arrayitem);

item = cjson_parse(pr);

name = cjson_getobjectitem(item,"name");

path = cjson_getobjectitem(item,"path");

flag = cjson_getobjectitem(item,"flag");

na = cjson_print(name);

pa = cjson_print(path);

fl = cjson_print(flag);

//printf("%s\n",pr);

printf("name:%s\n",na);

printf("path:%s\n",pa);

printf("flag:%s\n\n",fl);

intmain()

char *out1 = create1();

char *out2 = create2();

char *out3 = create3();

char *out4 = create4();

printf("%s\n\n\n",out1);

parse1(out1);

printf("%s\n\n\n",out2);

parse2(out2);

printf("%s\n\n\n",out3);

printf("%s\n\n\n",out4);

return 0;

執行結果如下圖所示:

上圖為建立json結構體和解析後的結果圖(分別為create1,parse1、create2,parse2),後兩個(create3、create4)建立了沒有進行解析,因為很簡單,自己動手試試吧!相信自己,有些事情其實還是會很容易做到的。

注:

cJSON的構造和解析

對於cjson的使用,我主要是用來模擬遠端伺服器端返回的乙個 json 型別的目錄結構,客戶端進行獲取並進行解析,把解析出來的目錄按照原本的結構顯示在本地。cjson 是乙個超輕巧,攜帶方便,單檔案,簡單的可以作為 ansi c 標準的json 解析器。進入cjson.h標頭檔案中可以檢視cjson...

cJSON的構建和解析

一 cjson構建 define module num 3 int encode json cjson root cjson createobject 在根節點建立cjson物件 root cjson name list array cjson createarray 建立陣列物件 name lis...

CJson使用(構造和解析)

cjson,目前來說,就只有兩個檔案,乙個cjson.c 乙個cjson.件。工程裡新增這兩個檔案就可以使用cjson構造或者解析相應的檔案。cjson cjson createobject 建立乙個json物件,返回乙個cjson結構體型別的指標。cjson cjson createarray 建...