二.创建json数据:
2.1 创建目标:形成以下json格式
{
"name": "darren",
"age": "25",
"subject": {
"math": "100",
"Chinese": "98"
},
"bankcard": [
{
"bankname": "CCB",
"bankcard": "123456",
"bankpwd": "543254"
}, {
"bankname": "ABC",
"bankcard": "23456",
"bankpwd": "543254"
}
]
}
2.2 以下为创建的代码:
#include
#include
#include
#include "cJSON.h"
int create_json(char* ret)
{
//创建根对象
cJSON *pJsonRoot = cJSON_CreateObject();
if( NULL == pJsonRoot )
{
printf("make pJsonRoot fail\n");
return -1;
}
//创建学科对象
cJSON *pJsonSubject = cJSON_CreateObject();
if( NULL == pJsonSubject )
{
printf("make pJsonSubject fail\n");
cJSON_Delete(pJsonRoot);
return -2;
}
//创建银行卡数组对象
cJSON *pJsonBankcard = cJSON_CreateArray();
if( NULL == pJsonBankcard )
{
printf("make pJsonBankcard fail\n");
cJSON_Delete(pJsonRoot);
cJSON_Delete(pJsonSubject);
return -3;
}
//创建建行卡对象
cJSON *pJsonCcb = cJSON_CreateObject();
if( NULL == pJsonCcb )
{
printf("make pJsonSubject fail\n");
cJSON_Delete(pJsonRoot);
cJSON_Delete(pJsonSubject);
cJSON_Delete(pJsonBankcard);
return -4;
}
//创建农行卡对象
cJSON *pJsonAbc = cJSON_CreateObject();
if( NULL == pJsonAbc )
{
printf("make pJsonSubject fail\n");
cJSON_Delete(pJsonRoot);
cJSON_Delete(pJsonSubject);
cJSON_Delete(pJsonBankcard);
cJSON_Delete(pJsonCcb);
}
/*
添加元素到建行卡对象中,组成:
{
"bandname": "CCB",
"bandcard": "123456",
"bandpwd": "543254"
}
*/
cJSON_AddStringToObject(pJsonCcb,"bankname","CCB");
cJSON_AddStringToObject(pJsonCcb,"bankcard","123456");
cJSON_AddStringToObject(pJsonCcb,"bankpwd","543254");
/*
添加元素到农行卡对象中,组成:
{
"bandname": "ABC",
"bandcard": "23456",
"bandpwd": "543254"
}
*/
cJSON_AddStringToObject(pJsonAbc,"bankname","ABC");
cJSON_AddStringToObject(pJsonAbc,"bankcard","23456");
cJSON_AddStringToObject(pJsonAbc,"bankpwd","543254");
/*
两个银行卡添加到数组中,组成:
[
{
"bandname": "CCB",
"bandcard": "123456",
"bandpwd": "543254"
}, {
"bandname": "ABC",
"bandcard": "23456",
"bandpwd": "543254"
}
]
*/
cJSON_AddItemToArray(pJsonBankcard,pJsonCcb);
cJSON_AddItemToArray(pJsonBankcard,pJsonAbc);
/*
添加数学、语文到学科对象,组成:
{
"math": "100",
"Chinese": "98"
}
*/
cJSON_AddStringToObject(pJsonSubject,"math","100");
cJSON_AddStringToObject(pJsonSubject,"Chinese","98");
/*
添加名字、岁数等String类型的元素到根对象
添加学科对象到根对象
添加银行卡数组对象到根对象
最终形成我们的目标json格式。
*/
cJSON_AddStringToObject(pJsonRoot,"name","darren");
cJSON_AddStringToObject(pJsonRoot,"age","25");
cJSON_AddItemToObject(pJsonRoot,"subject",pJsonSubject);
cJSON_AddItemToObject(pJsonRoot,"bankcard",pJsonBankcard);
char* out = cJSON_Print(pJsonRoot);
//printf("Json = %s\n",out);
memcpy(ret,out,strlen(out));
cJSON_Delete(pJsonRoot);
free(out);
return 0;
}
int main()
{
char buffer[2048] = "";
create_json(buffer);
printf("buffer = %s\n",buffer);
return 0;
}
//sign表层级
void print_json(cJSON* data, int sign)
{
if(!data)
return;
int count = sign;
while(count--)
{
printf("\t");
}
//对象类型为cJSON_String,打印name,value
if(data->type == cJSON_String)
{
printf("sign = %d: name = %s, value = %s\n",sign,data->string,data->valuestring);
}
//对象为 cJSON_Array 或 cJSON_Object
else if(data->type == cJSON_Array || data->type == cJSON_Object)
{
//打印name(数组里的元素是无name的)
printf("sign = %d: name = %s\n",sign,data->string);
int i = 0;
//获取对象里元素个数
for(; i < cJSON_GetArraySize(data); i++)
{
cJSON* item = cJSON_GetArrayItem(data,i);
//迭代,并增加层级
print_json(item,sign+1);
}
}
}
void process_json(char* data)
{
//字符串转json对象,使用结束后要delete掉,否则内存泄漏
cJSON* pRoot = cJSON_Parse(data);
if( pRoot == NULL )
{
printf("data is wrong\n");
return;
}
//假定知道json对象格式内容
/*
从pRoot集合,获取名称为"name"的对象,其类型为:cJSON_String
*/
cJSON* pName = cJSON_GetObjectItem(pRoot,"name");
printf("name=%s, value=%s\n",pName->string,pName->valuestring);
/*
从pRoot集合,获取名称为"subject"的对象,其类型为:cJSON_Object
*/
cJSON* pSubject = cJSON_GetObjectItem(pRoot,"subject");
if( pSubject->type == cJSON_Object )
{
/*
从pSubject集合,获取名称为"math"的对象,其类型为:cJSON_String
*/
cJSON* pMath = cJSON_GetObjectItem(pSubject,"math");
printf("name=%s, value=%s\n",pMath->string,pMath->valuestring);
}
/*
从pRoot集合,获取名称为"subject"的对象,其类型为:cJSON_Array
*/
cJSON* pBankcard = cJSON_GetObjectItem(pRoot,"bankcard");
if( pBankcard->type == cJSON_Array )
{
/*
从pBankcard数组,获取它第0个元素,其类型为:cJSON_Object
*/
cJSON* pArray0 = cJSON_GetArrayItem(pBankcard,0);
if( pArray0->type == cJSON_Object )
{
/*
从pBankcard数组第0元素,获取名称为"bankname"的对象,其类型为:cJSON_String
*/
cJSON* pBankName = cJSON_GetObjectItem(pArray0,"bankname");
printf("name=%s, value=%s\n",pBankName->string,pBankName->valuestring);
}
}
//假定未知json数据格式,逐个去获取并打印
print_json(pRoot,0);
//释放内存
cJSON_Delete(pRoot);
}
int main()
{
char buffer[2048] = "";
create_json(buffer);
printf("buffer = %s\n",buffer);
process_json(buffer);
return 0;
}