本文用代码简单介绍cjson的使用方法,1)创建json,从json中获取数据。2)创建json数组和解析json数组
1、 创建json,从json中获取数据
1 #include
2 #include "cJSON.h"
3
4 char *makeJson()5 {6 cJSON * pJsonRoot =NULL;7
8 pJsonRoot =cJSON_CreateObject();9 if(NULL ==pJsonRoot)10 {11 //error happend here
12 returnNULL;13 }14 cJSON_AddStringToObject(pJsonRoot, "hello", "hello world");15 cJSON_AddNumberToObject(pJsonRoot, "number", 10010);16 cJSON_AddBoolToObject(pJsonRoot, "bool", 1);17 cJSON * pSubJson =NULL;18 pSubJson =cJSON_CreateObject();19 if(NULL ==pSubJson)20 {21 //create object faild, exit
22 cJSON_Delete(pJsonRoot);23 returnNULL;24 }25 cJSON_AddStringToObject(pSubJson, "subjsonobj", "a sub json string");26 cJSON_AddItemToObject(pJsonRoot, "subobj", pSubJson);27