cJSON创建并解析json结构体

前端之家收集整理的这篇文章主要介绍了cJSON创建并解析json结构体前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

cJSON

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

cJSON简介

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

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

// cJSON结构体:

typedef struct cJSON {

struct cJSON *next,*prev; // next/prev allow you to walk array/object chains. Alternatively,use GetArraySize/GetArrayItem/GetObjectItem

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

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

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

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

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

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 类型:

#define cJSON_False 0

#define cJSON_True 1

#define cJSON_NULL 2

#define cJSON_Number 3

#define cJSON_String 4

#define cJSON_Array 5

#define cJSON_Object 6

具体用法

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

2函数用法详见cJSON.h注释

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

cJSON构造与解析json结构体

基本代码如下:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#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",".");

cJSON_AddStringToObject(dir1,"path","uploads/");

cJSON_AddStringToObject(dir1,"flag","true");

cJSON_AddItemToArray(root,dir2=cJSON_CreateObject());

cJSON_AddStringToObject(dir2,"..");

cJSON_AddStringToObject(dir2,"uploads");

cJSON_AddStringToObject(dir2,dir3=cJSON_CreateObject());

cJSON_AddStringToObject(dir3,"wang.txt");

cJSON_AddStringToObject(dir3,"uploads/wang.txt");

cJSON_AddStringToObject(dir3,"false");

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

out=cJSON_Print(root);

//删除

cJSON_Delete(root);

return out;

}

char * create2()

{

cJSON *root,*dir,*child,*subdir,*dir3;

char *out;

root=cJSON_CreateObject();

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

cJSON_AddStringToObject(dir,"/");

cJSON_AddStringToObject(dir,"true");

cJSON_AddItemToObject(root,"Child",subdir = cJSON_CreateArray());

cJSON_AddItemToObject(subdir,"dira",dir1=cJSON_CreateObject());

cJSON_AddStringToObject(dir1,"/./");

cJSON_AddStringToObject(dir1,"true");

cJSON_AddItemToObject(subdir,"/../");

cJSON_AddStringToObject(dir2,"uploads");

cJSON_AddStringToObject(dir3,"/uploads/");

cJSON_AddStringToObject(dir3,"true");

out=cJSON_Print(root);

cJSON_Delete(root);

return out;

}

char * create3()

{

cJSON *root,*img,*thm;

char *out;

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

root=cJSON_CreateObject();

cJSON_AddItemToObject(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,125);

cJSON_AddStringToObject(thm,"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,*dir2;

char *out;

const char *ro = "Root";

root=cJSON_CreateObject();

cJSON_AddItemToObject(root,ro,dir1=cJSON_CreateArray());

cJSON_AddNumberToObject(dir1,800);

cJSON_AddNumberToObject(dir1,600);

cJSON_AddStringToObject(dir1,"key and value");

cJSON_AddItemToObject(root,dir2=cJSON_CreateArray());

cJSON_AddNumberToObject(dir2,125);

cJSON_AddStringToObject(dir2,"100");

cJSON_AddStringToObject(dir2,"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<size;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,*flag;

int i = 0,*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",fl);

}

Child = cJSON_GetObjectItem(root,"Child");

if(Child)

{

size = cJSON_GetArraySize(Child);

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

printf("Child:\n");

for(i=0;i<size;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);

}

}

}

}

}

int main()

{

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,parse1create2,parse2),后两个(create3、create4)创建了没有进行解析,因为很简单,自己动手试试吧!相信自己,有些事情其实还是会很容易做到的。

原文链接:https://www.f2er.com/json/290461.html

猜你在找的Json相关文章