cjson格式举例
——组包
1.基本对象插入元素成员(空,真,假,bool,数字,字符串)
int test_cjson()
{
¦ int ret = 0;
¦ cJSON *root = cJSON_CreateObject();
¦ if(!root)
¦ {
¦ ¦ debug("root is null\n");
¦ ¦ return -1;
¦ }
¦ cJSON_AddStringToObject(root,"application","我的应用");
¦ cJSON_AddStringToObject(root,"port",port_cloud);
¦ cJSON_AddStringToObject(root,"destination","目标应用");
¦ cJSON *payload = cJSON_CreateObject();
¦ if(!payload)
¦ {
¦ ¦ debug("payload is null\n");
¦ ¦ if(root)
¦ ¦ {
¦ ¦ ¦ cJSON_Delete(root);
¦ ¦ ¦ root = NULL;
¦ ¦ }
¦ ¦ return -1;
¦ }
¦ cJSON_AddItemToObject(root,"payload",payload);
¦ cJSON_AddNullToObject(payload,"kong");
¦ cJSON_AddTrueToObject(payload,"zhen");
¦ cJSON_AddFalseToObject(payload,"jia");
¦ cJSON_AddBoolToObject(payload,"bu_er_1",true);
¦ cJSON_AddBoolToObject(payload,"bu_er_2",false);
¦ cJSON_AddNumberToObject(payload,"code",1234);
¦ cJSON_AddStringToObject(payload,"string","abcdefg");
//注意:这个函数里面调用了CJSON_Print(),使用完后需要释放root和out这两个指针;
//释放char型指针:free(out) 释放cjson型指针:cJSON_Delete(root)
¦ ret = mqtt_publish(root);
¦ debug("\n");
¦ if(ret != 0)
¦ {
¦ ¦ debug("publish error\\n");
¦ }
¦ return 0;
}
-----------------------------------------------------------------------------------------
{
"application": "我的应用",
"port": "ff",
"destination": "目标应用",
"payload": {
"kong": null,
"zhen": true,
"jia": false,
"bu_er_1": true,
"bu_er_2": false,
"code": 1234,
"string": "abcdefg"
}
}
需要注意的是 json 格式的数据,虽然也是一个字符串的样子,但这个时候还是无法当成普通的字符串进行使用,需要调用 cJSON_PrintUnformatted(root) 或者 cJSON_Print(root);来将json对象转换成普通的字符串,并且都是以该json对象的根为基点。两个API的区别即是:一个是没有格式的:也就是转换出的字符串中间不会有"\n" "\t"之类的东西存在,而cJSON_Print(root);打印出来是人看起来很舒服的格式
2.对象中带数组,数组里面包括对象,对象里面再添加各种类型的变量
int test_cjson()
{
¦ int ret = 0;
¦ debug("publish test message\n");
¦ cJSON *root = cJSON_CreateObject();
¦ if(!root)
¦ {
¦ ¦ debug("root is null\n");
¦ ¦ return -1;
¦ }
¦ cJSON_AddStringToObject(root,"application","我的应用");
¦ cJSON_AddStringToObject(root,"port",port_cloud);
¦ cJSON_AddStringToObject(root,"destination","目标应用");
¦ cJSON *payload = cJSON_CreateObject();
¦ if(!payload)
¦ {
¦ ¦ debug("payload is null\n");
¦ ¦ if(root)
¦ ¦ {
¦ ¦ ¦ cJSON_Delete(root);
¦ ¦ ¦ root = NULL;
¦ ¦ }
¦ ¦ return -1;
¦ }
¦ cJSON_AddItemToObject(root,"payload",payload);
¦ cJSON *test_array = cJSON_CreateArray();//需要判断是否申请内存成功
¦ cJSON_AddItemToObject(payload,"array",test_array);
¦ cJSON *object_array1 = cJSON_CreateObject();
¦ cJSON_AddItemToArray(test_array,object_array1);//需要判断是否申请内存成功
¦ cJSON_AddNullToObject(object_array1,"kong");
¦ cJSON *object_array2 = cJSON_CreateObject();
¦ cJSON_AddItemToArray(test_array,object_array2);//需要判断是否申请内存成功
¦ cJSON_AddTrueToObject(object_array2,"zhen");
¦ cJSON *object_array3 = cJSON_CreateObject();
¦ cJSON_AddItemToArray(test_array,object_array3);//需要判断是否申请内存成功
¦ cJSON_AddFalseToObject(object_array3,"jia");
¦ cJSON *object_array4 = cJSON_CreateObject();
¦ cJSON_AddItemToArray(test_array,object_array4);//需要判断是否申请内存成功
¦ cJSON_AddBoolToObject(object_array4,"bu_er_1",true);
¦ cJSON *object_array5 = cJSON_CreateObject();
¦ cJSON_AddItemToArray(test_array,object_array5);//需要判断是否申请内存成功
¦ cJSON_AddBoolToObject(object_array5,"bu_er_2",false);
¦ cJSON *object_array6 = cJSON_CreateObject();
¦ cJSON_AddItemToArray(test_array,object_array6);//需要判断是否申请内存成功
¦ cJSON_AddNumberToObject(object_array6,"code",1234);
¦ cJSON *object_array7 = cJSON_CreateObject();
¦ cJSON_AddItemToArray(test_array,object_array7);//需要判断是否申请内存成功
¦ cJSON_AddStringToObject(object_array7,"string","abcdefg");
cJSON_AddItemToArray(test_array,cjSON_CreateNumber(100));
cJSON_AddItemToArray(test_array,cjSON_CreateString(“aaaaaaa”));
¦ ret = mqtt_publish(root);
¦ debug("\n");
¦ if(ret != 0)
¦ {
¦ ¦ debug("publish error\n");
¦ }
¦ return 0;
}
----------------------------------------------------------------------------------------
{
"application": "我的应用",
"port": "ff",
"destination": "目标应用",
"payload": {
"array": [{
"kong": null
}, {
"zhen": true
}, {
"jia": false
}, {
"bu_er_1": true
}, {
"bu_er_2": false
}, {
"code": 1234
}, {
"string": "abcdefg"
},
100,
aaaaaa
]
}
}
3. 将字符指针串数组或者从数据库中获取的表格的数据以cjson格式组包发送
int test_cjson()
{
¦ int ret = 0;
¦ char *array[] = {"caofei","ni","hao","tian","tian","kai","xing"};
¦ debug("publish test message\n");
¦ cJSON *root = cJSON_CreateObject();
¦ if(!root)
¦ ¦ {
¦ ¦ ¦ debug("root is null\n");
¦ ¦ ¦ return -1;
¦ }
¦ cJSON_AddStringToObject(root,"application","我的应用");
¦ cJSON_AddStringToObject(root,"port",port_cloud);
¦ cJSON_AddStringToObject(root,"destination","目标应用");
¦ cJSON *payload = cJSON_CreateObject();
¦ if(!payload)
¦ {
¦ ¦ debug("payload is null\n");
¦ ¦ if(root)
¦ ¦ {
¦ ¦ ¦ cJSON_Delete(root);
¦ ¦ ¦ root = NULL;
¦ ¦ }
¦ ¦ return -1;
¦ }
¦ cJSON_AddItemToObject(root,"payload",payload);
¦ cJSON_AddItemToObject(payload,"array",cJSON_CreateStringArray(array,sizeof(array)/sizeof(array[0]) ));
¦
¦ ret = mqtt_publish(root);
¦ debug("\n");
¦ if(ret != 0)
¦ {
¦ ¦ debug("publish error\n");
¦ }
¦ return 0;
}
----------------------------------------------------------------------------------------
{
"application": "我的应用",
"port": "ff",
"destination": "目标应用",
"payload": {
"array": ["caofei", "ni", "hao", "tian", "tian", "kai", "xing"]
}
}
4.将二维整形数组组包
int test_cjson()
{
¦ int ret = 0;
¦ int numbers[3][3] = {{0,-1,0},{1,0,0},{0,0,1}};
¦ debug("publish test message\n");
¦ cJSON *root = cJSON_CreateObject();
¦ if(!root)
¦ ¦ {
¦ ¦ ¦ debug("root is null\n");
¦ ¦ ¦ return -1;
¦ }
¦ cJSON_AddStringToObject(root,"application","我的应用");
¦ cJSON_AddStringToObject(root,"port",port_cloud);
¦ cJSON_AddStringToObject(root,"destination","目标应用");
¦ cJSON *payload = cJSON_CreateObject();
¦ if(!payload)
¦ {
¦ ¦ debug("payload is null\n");
¦ ¦ if(root)
¦ ¦ {
¦ ¦ ¦ cJSON_Delete(root);
¦ ¦ ¦ root = NULL;
¦ ¦ }
¦ ¦ return -1;
¦ }
¦ cJSON_AddItemToObject(root,"payload",payload);
¦ int i;
¦ for(i=0; i<3; i++)
¦ {
¦ ¦ cJSON_AddItemToArray(payload,cJSON_CreateIntArray(numbers[i],sizeof(numbers)/sizeof(numbers[0])));
¦ }
¦ ret = mqtt_publish(root);
¦ debug("\n");
¦ if(ret != 0)
¦ {
¦ ¦ debug("publish error\n");
¦ }
¦ return 0;
}
-----------------------------------------------------------------------------------------
{
"application": "我的应用",
"port": "ff",
"destination": "目标应用",
"payload": {
: [0, -1, 0],
: [1, 0, 0],
: [0, 0, 1]
}
}
5.组一个复杂对象的包
int report_device()
{
int ret = 0;
char **resultp;
int nrow,ncolumn,i;
char *sql_cmd;
cJSON *device_1,*device_2,*device_3,*device_4,*device_5;
cJSON *attr_1,*attr_2,*attr_3,*attr_4,*attr_5;
char push_1[100] = "null",push_2[100] = "null",push_3[100] = "null",push_4[100] = "null",push_5[100] = "null";
debug("publish the device from database\n");
cJSON *root = cJSON_CreateObject();
if(!root)
{
debug("root is null\n");
return -1;
}
cJSON_AddStringToObject(root,"application",application);
cJSON_AddStringToObject(root,"port",port_cloud);
cJSON_AddStringToObject(root,"destination",destination);
cJSON *payload = cJSON_CreateObject();
if(!payload)
{
debug("payload is null\n");
if(root)
{
cJSON_Delete(root);
root = NULL;
}
return -1;
}
cJSON_AddItemToObject(root,"payload",payload);
cJSON_AddNumberToObject(payload,"code",107);
cJSON_AddNumberToObject(payload,"serial",12004);
cJSON *device = cJSON_CreateArray();
switch(1)
{
case 1:
cJSON_AddItemToArray(device,device_1 = cJSON_CreateObject());
cJSON_AddStringToObject(device_1,"name","机顶盒");
sql_cmd = sqlite3_mprintf("select * from STB;");
debug("sql_cmd : %s \n",sql_cmd);
if( sqlite3_get_table(db,sql_cmd,&resultp,&nrow,&ncolumn,&errmsg) == SQLITE_OK )
{
for(i=1; i<=nrow*ncolumn; i++)
{
if(i == 1)
{
strcpy(push_1,resultp[1]);
}
else
{
strcat(push_1,"-");
strcat(push_1,resultp[i]);
}
}
}
else
{
debug("error : %s\n",errmsg);
return -1;
}
cJSON_AddItemToObject(device_1,"act",cJSON_CreateString(push_1));
case 2:
cJSON_AddItemToArray(device,device_2 = cJSON_CreateObject());
cJSON_AddStringToObject(device_2,"name","电视机");
sql_cmd = sqlite3_mprintf("select * from TELEVITION;");
debug("sql_cmd : %s \n",sql_cmd);
if( sqlite3_get_table(db,sql_cmd,&resultp,&nrow,&ncolumn,&errmsg) == SQLITE_OK )
{
for(i=1; i<=nrow*ncolumn; i++)
{
if(i == 1)
{
strcpy(push_2,resultp[1]);
}
else
{
strcat(push_2,"-");
strcat(push_2,resultp[i]);
}
}
}
else
{
debug("error : %s\n",errmsg);
return -1;
}
cJSON_AddItemToObject(device_2,"act",cJSON_CreateString(push_2));
case 3:
cJSON_AddItemToArray(device,device_3 = cJSON_CreateObject());
cJSON_AddStringToObject(device_3,"name","风扇");
sql_cmd = sqlite3_mprintf("select * from FAN;");
debug("sql_cmd : %s \n",sql_cmd);
if( sqlite3_get_table(db,sql_cmd,&resultp,&nrow,&ncolumn,&errmsg) == SQLITE_OK )
{
for(i=1;i<=nrow*ncolumn;i++)
{
if(i == 1)
{
strcpy(push_3,resultp[1]);
}
else
{
strcat(push_3,"-");
strcat(push_3,resultp[i]);
}
}
}
else
{
debug("error : %s\n",errmsg);
return -1;
}
cJSON_AddItemToObject(device_3,"act",cJSON_CreateString(push_3));
case 4:
cJSON_AddItemToArray(device,device_4 = cJSON_CreateObject());
cJSON_AddStringToObject(device_4,"name","扫地机");
sql_cmd = sqlite3_mprintf("select * from SWEEPER;");
debug("sql_cmd : %s \n",sql_cmd);
if( sqlite3_get_table(db,sql_cmd,&resultp,&nrow,&ncolumn,&errmsg) == SQLITE_OK )
{
for(i=1;i<=nrow*ncolumn;i++)
{
if(i == 1)
{
strcpy(push_4,resultp[1]);
}
else
{
strcat(push_4,"-");
strcat(push_4,resultp[i]);
}
}
}
else
{
debug("error : %s\n",errmsg);
return -1;
}
cJSON_AddItemToObject(device_4,"act",cJSON_CreateString(push_4));
case 5:
cJSON_AddItemToArray(device,device_5 = cJSON_CreateObject());
cJSON_AddStringToObject(device_5,"name","背景音乐");
sql_cmd = sqlite3_mprintf("select * from BACKGROUND_MUSIC;");
debug("sql_cmd : %s \n",sql_cmd);
if( sqlite3_get_table(db,sql_cmd,&resultp,&nrow,&ncolumn,&errmsg) == SQLITE_OK )
{
for(i=1;i<=nrow*ncolumn;i++)
{
if(i == 1)
{
strcpy(push_5,resultp[1]);
}
else
{
strcat(push_5,"-");
strcat(push_5,resultp[i]);
}
}
}
else
{
debug("error : %s\n",errmsg);
return -1;
}
cJSON_AddItemToObject(device_5,"act",cJSON_CreateString(push_5));
break;
}
cJSON_AddItemToObject(payload,"device",device);
cJSON_AddNumberToObject(payload,"result",0);
ret = mqtt_publish(root);
debug("\n");
if(ret != 0)
{
debug("publish error\n");
}
return 0;
}
{
“application” : “uart_master”,
“port” : “ff”,
“destination” : “sz_central”,
“payload” : {
“code” : 1007,
“serial” : 12004,
“device” : [
{
“name” : “机顶盒”,
“act” : “开关-上-下-确定-主页-返回-菜单-音量加-音量减”
},
{
“name” : “电视机”,
“act” : “开关-上-下-确定-主页-返回-菜单-音量加-音量减”
},
{
“name” : “风扇”,
“act” : “开机-摇头-风量-定时-关机-风类-备用-备用2-备用3”
},
{
“name” : “扫地机”,
“act” : “回充-上-下-左-右-启动/暂停-边沿式-强劲式-规划式”
},
{
“name” : “背景音乐”,
“act” : “ ”//什么都没学
},
……
]
}
result : 0 //0表示成功,1表示失败
}
——解包
1.解析基本对象,对象中对象,对象中各种变量
{
"application": "我的应用",
"port": "ff",
"destination": "目标应用",
"payload": {
"kong": null,
"zhen": true,
"jia": false,
"bu_er_1": true,
"bu_er_2": false,
"code": 1234,
"string": "abcdefg"
}
}
int test_get(cJSON *root)
{
¦ if(!root)
¦ {
¦ ¦ debug("get root faild !\n");
¦ ¦ return -1;
¦ }
¦ cJSON *application = cJSON_GetObjectItem(root,"application");
¦ if(!application)
¦ {
¦ ¦ debug("No application !\n");
¦ ¦ return -1;
¦ }
¦ printf("application is %s type is %d\n",application->valuestring,application->type);
¦ cJSON *port = cJSON_GetObjectItem(root,"port");
¦ if(!port)
¦ {
¦ ¦ debug("No port !\n");
¦ ¦ return -1;
¦ }
¦ printf("port is %s type is %d\n",port->valuestring,port->type);
¦ cJSON *destination = cJSON_GetObjectItem(root,"destination");
¦ if(!destination)
¦ {
¦ ¦ debug("No destination !\n");
¦ ¦ return -1;
¦ }
¦ printf("destination is %s type is %d\n",destination->valuestring,destination->type);
¦ cJSON *payload = cJSON_GetObjectItem(root,"payload");
¦ cJSON *kong = cJSON_GetObjectItem(payload,"kong");
¦ cJSON *zhen = cJSON_GetObjectItem(payload,"zhen");
¦ cJSON *jia = cJSON_GetObjectItem(payload,"jia");
¦ cJSON *bu_er_1 = cJSON_GetObjectItem(payload,"bu_er_1");
¦ cJSON *bu_er_2 = cJSON_GetObjectItem(payload,"bu_er_2");
¦ cJSON *code = cJSON_GetObjectItem(payload,"code");
¦ cJSON *string = cJSON_GetObjectItem(payload,"string");
¦ printf("kong is %s type is %d\n",kong->string,kong->type);
¦ printf("zhen is %s type is %d\n",zhen->string,zhen->type);
¦ printf("jia is %s type is %d\n",jia->string,jia->type);
¦ printf("bu_er_1 is %d type is %d\n",bu_er_1->valueint,bu_er_1->type);
¦ printf("bu_er_2 is %d type is %d\n",bu_er_2->valueint,bu_er_2->type);
¦ printf("code is %d type is %d\n",code->valueint,code->type);
¦ printf("string is %s type is %d,string is %s\n",string->valuestring,string->type,string->string);
¦ return 0;
}
2.解析对象的成员,解析数组,解析数组中的对象
{
"application": "我的应用",
"port": "ff",
"destination": "目标应用",
"payload": {
"array": [{
"string": "aaaaaaa"
}, {
"string": "bbbbbbb"
}, {
"string": "ccccccc"
}, {
"string": "ddddddd"
}, {
"string": "eeeeeee"
}, {
"string": "fffffff"
}, {
"string": "ggggggg"
}]
}
}
int test_get(cJSON *root)
{
¦ if(!root)
¦ {
¦ ¦ debug("get root faild!\n");
¦ ¦ return -1;
¦ }
¦ cJSON *application = cJSON_GetObjectItem(root,"application");
¦ if(!application)
¦ {
¦ ¦ debug("No application!\n");
¦ ¦ return -1;
¦ }
¦ printf("application is %s type is %d\n",application->valuestring,application->type);
¦ cJSON *port = cJSON_GetObjectItem(root,"port");
¦ if(!port)
¦ {
¦ ¦ debug("No port! \n");
¦ ¦ return -1;
¦ }
¦ printf("port is %s type is %d\n",port->valuestring,port->type);
¦ cJSON *destination = cJSON_GetObjectItem(root,"destination");
¦ if(!destination)
¦ {
¦ ¦ debug("No destination! \n");
¦ ¦ return -1;
¦ }
¦ printf("destination is %s type is %d\n",destination->valuestring,destination->type);
¦
¦ cJSON *payload = cJSON_GetObjectItem(root,"payload");
¦ if(!payload)
¦ {
¦ ¦ printf("No payload!\n");
¦ ¦ return -1;
¦ }
¦ cJSON *array = cJSON_GetObjectItem(payload,"array");
¦ int array_size = cJSON_GetArraySize(array);
¦ int i = 0;
¦ cJSON *item,*item_string;
¦ printf("array size is %d\n",array_size);
¦ for(i=0; i<array_size; i++)
¦ {
¦ ¦ item = cJSON_GetArrayItem(array,i);
¦ ¦ item_string = cJSON_GetObjectItem(item,"string");
¦ ¦ printf("item type is %d\n",item->type);
¦ ¦ printf("string is %s\n",item_string->valuestring);
¦ }
¦ return 0;
}
3.将字符串格式数据转换成cjson格式的数据
char *s = "{\"name\":\"xiao hong\",\"age\":10}";
cJSON *root = cJSON_Parse(s);
——关于cjson格式数据收发中编程细节
1.获得的cjson数据整体在函数之间的传参
void group_state(cJSON *tmpSub0,char *cloudName)
{
printf("*****************this is group_state*****************\n");
cJSON *root,*payload;
cJSON *pcontrol;
cJSON *control;
char *addr_str,*sql_cmd,*out;
char **resultp;
int nrow,ncolumn,i;
root = cJSON_CreateObject();
cJSON_AddStringToObject(root,"application",myName);
cJSON_AddStringToObject(root,"port",myPort_str);
cJSON_AddStringToObject(root,"destination",cloudName);
cJSON_AddItemToObject(root,"payload",payload=cJSON_CreateObject());
cJSON_AddNumberToObject(payload,"code",203);
cJSON_AddNumberToObject(payload,"gid",rec_gid);
cJSON_AddNumberToObject(payload,"serial",rec_serial);
control = cJSON_GetObjectItem(tmpSub0,"control");
control_thread = cJSON_Duplicate(control,1);
printf("***********......%s****************\n",cJSON_Print(control));
printf("***********......%s****************\n",cJSON_Print(control_thread));
if(control)
{
pcontrol = cJSON_Duplicate(control,1);
sql_cmd = sqlite3_mprintf("select * from group_table where gid=%d;",rec_gid);
debug_print("sql_cmd :%s \n",sql_cmd);
if(sqlite3_get_table(db,sql_cmd,&resultp,&nrow,&ncolumn,&errmsg) != SQLITE_OK)
{
printf("error:%s\n",errmsg);
cJSON_AddItemToObject(payload,"control",pcontrol);
cJSON_AddNumberToObject(payload,"result",1);
out = cJSON_Print(root);
cJSON_Delete(root);
cJSON_Minify(out);
gwMqttPub(out,__FUNCTION__);
free(out);
return;
}
2.利用cjson的链表功能来访问数据
cJSON *ebelong_devjoin = NULL;
int add_to_ebelonglist(uint8_t *ebelong_devid,uint16_t ebelong_devep)
{
if(searchin_ebelonglist(ebelong_devid,ebelong_devep) == -1)//等于-1 就是没有这个设备
{
cJSON *ebelong_devices = cJSON_CreateObject();
cJSON_AddStringToObject(ebelong_devices, "id", ebelong_devid);
cJSON_AddNumberToObject(ebelong_devices, "ep", ebelong_devep);
cJSON_AddItemToArray(ebelong_devjoin, ebelong_devices);//将新来的设备加到cjson设备链表中
debug("add into ebelong_devjoin:%s , %02x\n",ebelong_devid,ebelong_devep);
}
else
{
debug("device %s %02x exist or ebelong_devjoin is NULL\n",ebelong_devid,ebelong_devep);
}
return 0;
}
3.cjson如果组包或者解包格式不等时会有段错误
4.关于cjson中申请和释放内存问题
5.创建的cjson格式的数据在代码中需要判断是否创建成功,需要增加代码的健壮性;