cJSON是一个超轻巧,携带方便,单文件,简单的可以作为ANSI-C标准的JSON解析器。
进入cJSON.h头文件中可以查看cJSON的相关信息。主要包括:cJSON结构体、cJSON类型、cJSON的一些内部的函数等。
// cJSON结构体:
typedefstructcJSON {
} 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一起编译即可使用。
更多介绍机器使用请参考:http://sourceforge.net/projects/cjson/.
cJSON构造与解析json结构体
基本代码如下:
#include
#include"cJSON.h"
char* create1()
{
"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());
"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());
"uploads/wang.txt");
"false");
}
char* create2()
"Child",subdir = cJSON_CreateArray());
"/../");
"/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;
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());
"value",600);
"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;
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());
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());
voidparse1(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,pr);
"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;
intmain()
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);
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);
}
运行结果如下图所示:
上图为创建json结构体和解析后的结果图(分别为create1,parse1、create2,parse2),后两个(create3、create4)创建了没有进行解析,因为很简单,自己动手试试吧!相信自己,有些事情其实还是会很容易做到的。
注: