cjson构建_JSON构造/解析(by C)---cJSON和json-c

#include

#include

#include

#include "cJSON.h"

void printJson(cJSON *root)

{

if(!root)

{

printf("NULL JSON root.\n");

return;

}

printf("Type=0x%02x, %s=%s\n", root->type, root->string, cJSON_Print(root));  cJSON_Print会根据类型打印

}

static char * makeJson(void)

{

cJSON *pJsonRoot = NULL;

cJSON *pSubJson = NULL;

char *p = NULL;

pJsonRoot = cJSON_CreateObject();

if(NULL == pJsonRoot)

{

printf("%s line=%d NULL\n", __func__, __LINE__);

return NULL;

}

cJSON_AddStringToObject(pJsonRoot, "hello", "hello world");  String类型    cJSON_AddNumberToObject(pJsonRoot, "number", 10010);  Number类型    cJSON_AddBoolToObject(pJsonRoot, "bool", 1);  bool类型

pSubJson = cJSON_CreateObject();  创建一个cJSON,用于嵌套数据

if(NULL == pSubJson)

{

printf("%s line=%d NULL\n", __func__, __LINE__);

cJSON_Delete(pJsonRoot);

return NULL;

}

cJSON_AddStringToObject(pSubJson, "subjsonobj", "a sub json string");  在子cJSON下,增加一个String类型数据

cJSON_AddItemToObject(pJsonRoot, "subobj", pSubJson);  将子cJSON加入到pJsonRoot

p = cJSON_Print(pJsonRoot);

if(NULL == p)

{

printf("%s line=%d NULL\n", __func__, __LINE__);

cJSON_Delete(pJsonRoot);

return NULL;

}

cJSON_Delete(pJsonRoot);

return p;

}

static void parseJson(char * pMsg)

{

cJSON *pJson;

cJSON *pSub;

cJSON * pSubSub;

if(NULL == pMsg)

{

return;

}

pJson = cJSON_Parse(pMsg);

if(NULL == pJson)

{

return ;

}

pSub = cJSON_GetObjectItem(pJson, "hello");

printJson(pSub);

pSub = cJSON_GetObjectItem(pJson, "number");

printJson(pSub);

pSub = cJSON_GetObjectItem(pJson, "bool");

printJson(pSub);

pSub = cJSON_GetObjectItem(pJson, "subobj");

printJson(pSub);

pSubSub = cJSON_GetObjectItem(pSub, "subjsonobj");

printJson(pSubSub);

cJSON_Delete(pJson);

}

int main(void)

{

char *p;

/* print the version */

printf("Version: %s\n", cJSON_Version());  获取cJSON版本号

p = makeJson();  构造JSON数据

if(NULL == p)

{

return 0;

}

printf("p = \n%s\n\n", p);  打印构造的字符串

parseJson(p);  解析JSON数据,并打印Type-Key-Value

free(p);

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值