字符串(string)是由双引号包围的任意数量Unicode字符的集合,使用反斜线转义。一个字符(character)即一个单独的字符串(character string)。
字符串(string)与C或者Java的字符串非常相似。
{
"name": "Jack (\"Bee\") Nimble",
"format": {
"type": "rect",
"width": 1920,
"height": 1080,
"he": 1,
"interlace": false,
"frame rate": 24
}
}
cJSON *root1,*fmt;
root1=cJSON_CreateObject();
cJSON_AddItemToObject(root1, "name", cJSON_CreateString("Jack (\"Bee\") Nimble")); cJSON_AddItemToObject(root, "format", fmt = cJSON_CreateObject()); //fmt 嵌套的object cJSON_AddStringToObject(fmt, "type", "rect"); //添加string类型 cJSON_AddNumberToObject(fmt, "width", 1920); cJSON_AddNumberToObject(fmt, "height", 1080); cJSON_AddNumberToObject(fmt, "he", 1); //添加数字类型 cJSON_AddFalseToObject (fmt, "interlace"); //添加bool类型 cJSON_AddNumberToObject(fmt, "frame rate", 24); // cJSON_AddTrueToObject(fmt, "book"); //添加bool cJSON_AddItemToObject(root1, "ip", cJSON_Createu8Array(ip, 3)); //添加数组 memcpy(out_buff, cJSON_Print(root1), strlen(cJSON_Print(root1))); //将root1json 对象转为数组 cJSON_Print->格式输出 cJSON_PrintUnformatted->无格式输出 cJSON_Delete(root1); //释放内存 free(out); //释放内存 cJSON_AddItemToObject(root1, "name", cJSON_CreateString("Jack (\"Bee\") Nimble"));
//下面输出的实际结果
0A 09 //换行 制表符 空格9个
0A 02 //换行 正文开始 空格2个
//json键值输出
void printJson(cJSON * root)//以递归的方式打印json的最内层键值对
{
for(int i=0; i
{
cJSON * item = cJSON_GetArrayItem(root, i);
if(cJSON_Object == item->type) //如果对应键的值仍为cJSON_Object就递归调用printJson
printJson(item);
else //值不为json对象就直接打印出键和值
{
printf("%s->", item->string);
printf("%s\n", cJSON_PrintUnformatted(item));//cJSON_Print
}
}
}
//cjson 数据解析与构造
void cJSON_Demo(void)
{
cJSON * root = NULL;
cJSON * item = NULL;//cjson对象
cJSON *root1,*fmt;
char * jsonStr = "{\"semantic\":{\"slots\":{\"name\":\"张三\"}},\"rc\":0,\"operation\":\"CALL\",\"service\":\"telephone\"}";
char *jso_buff,*out = NULL;
uint8_t out_buff[1000],ip[5];
ip[0] = 1;
ip[1] = 2;
ip[2] = 3;
printf("Cjson版本号:%s\r\n",cJSON_Version());
printf("\r\n");
HAL_Delay(2000);
root = cJSON_Parse(jsonStr);
if (!root)
{
printf("Error before: [%s]\n",cJSON_GetErrorPtr());
}
else
{
printf("%s\n", "有格式的方式打印Json:");
printf("%s\n\n", cJSON_Print(root));
printf("%s\n", "无格式方式打印json:");
printf("%s\n\n", cJSON_PrintUnformatted(root));
HAL_Delay(2000);
printf("%s\n", "一步一步的获取name 键值对:");
printf("%s\n", "获取semantic下的cjson对象:");
item = cJSON_GetObjectItem(root, "semantic");
printf("%s\n", cJSON_Print(item));
printf("%s\n", "获取slots下的cjson对象");
item = cJSON_GetObjectItem(item, "slots");
printf("%s\n", cJSON_Print(item));
printf("%s\n", "获取name下的cjson对象");
item = cJSON_GetObjectItem(item, "name");
printf("%s\n", cJSON_Print(item));
printf("%s\n", "获取rc键值");
item = cJSON_GetObjectItem(root, "rc");
printf("%s\n", cJSON_Print(item));
printf("%s\n", "获取operation键值");
item = cJSON_GetObjectItem(root, "operation");
printf("%s\n", cJSON_Print(item));
printf("%s:", item->string); //看一下cjson对象的结构体中这两个成员的意思
printf("%s\n", item->valuestring);
printf("\n%s\n", "打印json所有最内层键值对:");
printJson(root);
jso_buff = cJSON_Print(root);
cJSON_Delete(root);
printf( "json的数据为:%s\r\n",jso_buff);
HAL_Delay(2000);
printf("时间:%d",HAL_GetTick());
root1=cJSON_CreateObject();
cJSON_AddItemToObject(root1, "name", cJSON_CreateString("Jack (\"Bee\") Nimble")); //添加字符串 有转义符 \
cJSON_AddStringToObject(root1, "name", "Jack (\"Bee\") Nimble)); //这个是错误的
cJSON_AddItemToObject(root, "format", fmt=cJSON_CreateObject()); //添加object对象
cJSON_AddStringToObject(fmt,"type", "rect"); //添加字符串
cJSON_AddNumberToObject(fmt,"width", 1920);
cJSON_AddNumberToObject(fmt,"height", 1080);
cJSON_AddNumberToObject(fmt,"he", 1);//添加数字
cJSON_AddFalseToObject (fmt,"interlace"); //添加bool
cJSON_AddNumberToObject(fmt,"frame rate", 24);
cJSON_AddTrueToObject(fmt,"book");//添加bool
cJSON_AddItemToObject(root1,"ip",cJSON_Createu8Array(ip,3));//添加数组
printf("时间:%d,",HAL_GetTick());
out =cJSON_Print(root1); // cJSON_PrintBuffered(root1,140,0);
printf("%s\n",out); //有格式输出
printf("%s\n",cJSON_PrintUnformatted(root1)); //无格式输出 适合单片机
memcpy(out_buff,cJSON_Print(root1),strlen(cJSON_Print(root1)));
cJSON_Delete(root1); //释放内存
free(out); //释放内存
HAL_Delay(2000);
printf("接收的数据:%s\r\n",jsonStr);
}
}
PS:有道云笔记链接
添加的内容:(2018.4.17)
移植到STM32平台上时,运行一会,出现数据异常,如下图:
使用
printf("%s\n",cJSON_PrintUnformatted(root));
一看时内存不够了:
cJSON *root = NULL;
char *send_buff = NULL;
root = cJSON_CreateObject();
switch(Set_8266_information.WIFI_MODE)
{
case STA:
cJSON_AddStringToObject(root,"WIFI_MODE","STA");
break;
case AP:
cJSON_AddStringToObject(root,"WIFI_MODE","AP");
break;
case STA_AP:
cJSON_AddStringToObject(root,"WIFI_MODE","STA_AP");
break;
}
cJSON_AddItemToObject(root,"AP_IP",cJSON_CreateString((char *)&Set_8266_information.AP_IP));
cJSON_AddItemToObject(root,"AP_MAC",cJSON_CreateString((char *)&Set_8266_information.AP_MAC));
cJSON_AddItemToObject(root,"STA_IP",cJSON_CreateString((char *)&Set_8266_information.STA_IP));
cJSON_AddItemToObject(root,"STA_MAC",cJSON_CreateString((char *)&Set_8266_information.STA_MAC));
send_buff = cJSON_PrintUnformatted(root);//无格式输出适合单片机
//printf("%s\n",cJSON_PrintUnformatted(root));//无格式输出适合单片机
print_send((uint8_t *)send_buff,Cjson_buff_num);
cJSON_Delete(root);//释放内存
free(send_buff);//必须释放内存 否则出错!
问题解决,测试了一个10min效果。