cJSON的构造和解析

对于cJSON的使用,我主要是用来模拟远程服务器端返回的一个json类型的目录结构,客户端进行获取并进行解析,把解析出来的目录按照原本的结构显示在本地。

cJSON一个超轻巧,携带方便,单文件,简单的可以作为ANSI-C标准的JSON解析器。

进入cJSON.h文件中可以查看cJSON的相关信息。主要包括cJSON结构体、cJSON类型、cJSON的一些内部的函数等。

// cJSON结构体:

typedefstructcJSON {

structcJSON *next,*prev;// next/prev allow you to walk array/object chains. Alternatively,use GetArraySize/GetArrayItem/GetobjectItem

structcJSON *child;// An array or object item will have a child pointer pointing to a chain of the items in the array/object.

inttype;// The type of the item,as above.

char*valuestring;// The item's string,if type==cJSON_String

intvalueint;// The item's number,if type==cJSON_Number

doublevaluedouble;// The item's number,231)"> char*string;// The item's name string,if this item is the child of,or is in the list of subitems of an object.

} cJSON;

// cJSON类型:

#definecJSON_False 0

#definecJSON_True 1

#definecJSON_NULL 2

#definecJSON_Number 3

#definecJSON_String 4

#definecJSON_Array 5

#definecJSON_Object 6

用法

1、需要包含cJSON.h文件,然后和cJSON.c或库文件libcJSON.a一起编译即可使用。

2、具体函数用法详见注释

更多介绍机器使用请参考:http://sourceforge.net/projects/cjson/.

cJSON构造与解析json结构体

基本代码如下:

#include

#include"cJSON.h"

char* create1()

{

cJSON *root,*dir1,*dir2,*dir3;

char*out;

//创建json数组型结构体

root = cJSON_CreateArray();

//为数组添加对象

cJSON_AddItemToArray(root,dir1=cJSON_CreateObject());

//为对象添加字符串键值对

cJSON_AddStringToObject(dir1,"name",21)">".");

"path",21)">"uploads/");

"flag",21)">"true");

ottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:21px; color:rgb(70,dir2=cJSON_CreateObject());

cJSON_AddStringToObject(dir2,21)">"..");

"uploads");

ottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:21px; color:rgb(70,dir3=cJSON_CreateObject());

cJSON_AddStringToObject(dir3,21)">"wang.txt");

"uploads/wang.txt");

"false");

//json结构体转换为字符串

out=cJSON_Print(root);

//删除

cJSON_Delete(root);

returnout;

}

char* create2()

root=cJSON_CreateObject();

cJSON_AddItemToObject(root,21)">"Root",dir=cJSON_CreateObject());

cJSON_AddStringToObject(dir,21)">"/");

"Child",subdir = cJSON_CreateArray());

cJSON_AddItemToObject(subdir,21)">"dira",21)">"/./");

"/../");

"/uploads/");

char* create3()

ottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:21px; color:rgb(70,*img,*thm;

intnums[4]={100,200,300,400};

ottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:21px; color:rgb(70,img=cJSON_CreateObject());

cJSON_AddNumberToObject(img,21)">"key",800);

"value",600);

cJSON_AddStringToObject(img,21)">"Title",21)">"Sugon");

cJSON_AddItemToObject(img,21)">"child",thm=cJSON_CreateObject());

cJSON_AddNumberToObject(thm,125);

cJSON_AddStringToObject(thm,21)">"100");

cJSON_AddStringToObject(thm,21)">"Url",21)">"www.sugon.com");

"nums",cJSON_CreateIntArray(nums,4));

char* create4()

ottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:21px; color:rgb(70,*dir2;

constchar*ro ="Root";

ottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:21px; color:rgb(70,ro,dir1=cJSON_CreateArray());

cJSON_AddNumberToObject(dir1,21)">"key and value");

ottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:21px; color:rgb(70,dir2=cJSON_CreateArray());

cJSON_AddNumberToObject(dir2,21)">"value and key");

voidparse1(char*out)

cJSON * root,*arrayItem,*item,*name,*path,*flag;

inti = 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,21)">"name");

path = cJSON_GetobjectItem(item,21)">"path");

flag = cJSON_GetobjectItem(item,21)">"flag");

na = cJSON_Print(name);

pa = cJSON_Print(path);

fl = cJSON_Print(flag);

ottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:21px; color:rgb(70,pr);

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

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

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

}

}

voidparse2(char*out)

ottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:21px; color:rgb(70,*Root,*Child,*flag;

if(root)

Root = cJSON_GetobjectItem(root,21)">"Root");

if(Root)

name = cJSON_GetobjectItem(Root,231)"> path = cJSON_GetobjectItem(Root,231)"> flag = cJSON_GetobjectItem(Root,21)">"Root:\n");

Child = cJSON_GetobjectItem(root,21)">"Child");

if(Child)

size = cJSON_GetArraySize(Child);

//printf("%d\n",21)">"Child:\n");

for(i=0;i

{

arrayItem = cJSON_GetArrayItem(Child,231)"> if(arrayItem)

{

//printf("%s\n",231)"> pr = cJSON_Print(arrayItem);

item = cJSON_Parse(pr);

name = cJSON_GetobjectItem(item,231)"> path = cJSON_GetobjectItem(item,231)"> flag = cJSON_GetobjectItem(item,231)"> na = cJSON_Print(name);

pa = cJSON_Print(path);

fl = cJSON_Print(flag);

printf( }

}

}

intmain()

char*out1 = create1();

char*out2 = create2();

char*out3 = create3();

char*out4 = create4();

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

parse1(out1);

ottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:21px; color:rgb(70,out2);

parse2(out2);

ottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:21px; color:rgb(70,out3);

ottom:5px; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:21px; color:rgb(70,out4);

return0;

}

运行结果如下图所示:


上图为创建json结构体和解析后的结果图(分别为create1parse1create2parse2),后两个(create3create4)创建了没有进行解析,因为很简单,自己动手试试吧!相信自己,有些事情其实还是会很容易做到的。


注:

技术在于交流、沟通,转载请注明出处并保持作品的完整性。

相关文章

AJAX是一种基于JavaScript和XML的技术,能够使网页实现异步交...
在网页开发中,我们常常需要通过Ajax从后端获取数据并在页面...
在前端开发中,经常需要循环JSON对象数组进行数据操作。使用...
AJAX(Asynchronous JavaScript and XML)是一种用于创建 We...
AJAX技术被广泛应用于现代Web开发,它可以在无需重新加载页面...
Ajax是一种通过JavaScript和HTTP请求交互的技术,可以实现无...