加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cJSON的构造和解析

发布时间:2020-12-16 18:45:28 所属栏目:百科 来源:网络整理
导读:对于 cJSON 的使用,我主要是用来模拟远程服务器端返回的一个 json 类型的目录结构,客户端进行获取并进行解析,把解析出来的目录按照原本的结构显示在本地。 cJSON 是 一个超轻巧,携带方便,单文件,简单的可以作为 ANSI-C 标准的 JSON 解析器。 进入 cJSO

对于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");

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

"uploads");

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()

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

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()

constchar*ro ="Root";

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

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("%dn",size);

//遍历数组

for(i=0;i

{

//获取第i个数组项

arrayItem = cJSON_GetArrayItem(root,i);

if(arrayItem)

{

//printf("%sn","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);

printf("name:%sn",na);

"path:%sn",pa);

"flag:%snn",fl);

}

}

voidparse2(char*out)

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("%dn",21)">"Child:n");

for(i=0;i

{

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

{

//printf("%sn",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("%snnn",out1);

parse1(out1);

parse2(out2);

return0;

}

运行结果如下图所示:


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


注:

技术在于交流、沟通,转载请注明出处并保持作品的完整性。
作者: ╰☆奋斗ing?孩子` 原文: http://blog.sina.com.cn/s/blog_a6fb6cc90101ffme.html。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读