您的位置:首页 > 编程语言 > C语言/C++

使用Cjson在C语言进行Json的创建和解析

2016-10-11 13:44 465 查看
1,创建json,从json中获取数据。

//CJSON在内存中的存储方式是用链表进行存储的,所以在进行操作的时候,我们可见的部分全部是用指针进行操作的。
#include <stdio.h>
#include "cJSON.h"

char * makeJson()
{
cJSON * pJsonRoot = NULL;
pJsonRoot = cJSON_CreateObject(); //新建一个JSON项目:pSubJson
if(NULL == pJsonRoot)
{
//error happend here
return NULL;
}
//add 字符串、数字和bool变量
cJSON_AddStringToObject(pJsonRoot, "hello", "hello world");
cJSON_AddNumberToObject(pJsonRoot, "number", 10010);
cJSON_AddBoolToObject(pJsonRoot, "bool", 1);
cJSON * pSubJson = NULL;  //新建一个JSON项目:pSubJson。
pSubJson = cJSON_CreateObject();
if(NULL == pSubJson)
{
// create object faild, exit
cJSON_Delete(pJsonRoot);
return NULL;
}
// pSubJson项目上添加字符串。
cJSON_AddStringToObject(pSubJson, "subjsonobj", "a sub json string");
// 新项目添加到最初的项目pJsonRoot上
cJSON_AddItemToObject(pJsonRoot, "subobj", pSubJson);

char * p = cJSON_Print(pJsonRoot);
// else use :
// char * p = cJSON_PrintUnformatted(pJsonRoot);
if(NULL == p)
{
//convert json list to string faild, exit
//because sub json pSubJson han been add to pJsonRoot, so just delete pJsonRoot, if you also delete pSubJson, it will coredump, and error is : double free
cJSON_Delete(pJsonRoot);
return NULL;
}
//free(p);

cJSON_Delete(pJsonRoot);

return p;
}

void parseJson(char * pMsg)
{
if(NULL == pMsg)
{
return;
}
cJSON * pJson = cJSON_Parse(pMsg);
if(NULL == pJson)
{
// parse faild, return
return ;
}

// get string from json
cJSON * pSub = cJSON_GetObjectItem(pJson, "hello");
if(NULL == pSub)
{
//get object named "hello" faild
}
printf("obj_1 : %s\n", pSub->valuestring);

// get number from json
pSub = cJSON_GetObjectItem(pJson, "number");
if(NULL == pSub)
{
//get number from json faild
}
printf("obj_2 : %d\n", pSub->valueint);

// get bool from json
pSub = cJSON_GetObjectItem(pJson, "bool");
if(NULL == pSub)
{
// get bool from json faild
}
printf("obj_3 : %d\n", pSub->valueint);

// get sub object
pSub = cJSON_GetObjectItem(pJson, "subobj");
if(NULL == pSub)
{
// get sub object faild
}
cJSON * pSubSub = cJSON_GetObjectItem(pSub, "subjsonobj");
if(NULL == pSubSub)
{
// get object from subject object faild
}
printf("sub_obj_1 : %s\n", pSubSub->valuestring);

cJSON_Delete(pJson);
}

int main()
{
char * p = makeJson();
if(NULL == p)
{
return 0;
}
printf("%s\n", p);
parseJson(p);
  free(p);  //千万不要忘记释放内存呀,cJSON_Print()函数或者cJSON_PrintUnformatted()产生的内存,使用free(char *)进行释放
return 0;
}


运行结果:

// 创建的JSon字符串

{

“hello”: “hello world”,

“number”: 10010,

“bool”: true,

“subobj”: {

“subjsonobj”: “a sub json string”

}

}

// 从Json解析得到的数据

obj_1 : hello world

obj_2 : 10010

obj_3 : 1

sub_obj_1 : a sub json string

2,创建json数组和解析json数组。

//创建数组,数组值是另一个JSON的item,这里使用数字作为演示
char * makeArray(int iSize)
{
cJSON * root =  cJSON_CreateArray();
if(NULL == root)
{
printf("create json array faild\n");
return NULL;
}
int i = 0;

for(i = 0; i < iSize; i++)
{
cJSON_AddNumberToObject(root, "hehe", i);
}
char * out = cJSON_Print(root);
cJSON_Delete(root);

return out;
}

//解析刚刚的CJSON数组
void parseArray(char * pJson)
{
if(NULL == pJson)
{
return ;
}
cJSON * root = NULL;
if((root = cJSON_Parse(pJson)) == NULL)
{
return ;
}
int iSize = cJSON_GetArraySize(root);
for(int iCnt = 0; iCnt < iSize; iCnt++)
{
cJSON * pSub = cJSON_GetArrayItem(root, iCnt);
if(NULL == pSub)
{
continue;
}
int iValue = pSub->valueint;
printf("value[%2d] : [%d]\n", iCnt, iValue);
}
cJSON_Delete(root);
return;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  json