cjson笔记

前言
JSON ( JavaScript  Object Notation, JS 对象标记) 是一种轻量级的数据交换格式。它基于  ECMAScript  (欧洲计算机协会制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。
JSON是什么
通过阅读下面的页面来了解JSON的信息: http://www.json.org/json-zh.html
cJSON的源代码 http://sourceforge.net/projects/cjson/
可以了解到JSON具有两种形式,一种是由键值对(key/value)集合组成的对象(object),另一种是由值(value)的集合组成的数组。其中,key的类型是字符串类型,因为JSON允许结构嵌套,所以value的类型则有很多种,包括:字符串,数字,对象,数组,true,false,null

网络上不错的JSON文章

https://blog.csdn.net/kylinlinlinlin/article/details/44918661 //整个函数的解析

http://www.cnblogs.com/zengjfgit/p/4314330.html 源代码解析
https://blog.csdn.net/zhengnianli/article/details/79223457 Cjson 天气解析
JSON操作步骤
https://www.cnblogs.com/lang5230/p/5492702.html 数据解析的函数
1.json格式
对象是一个无序的“‘名称/值’对”集合。一个对象以“{”(左括号)开始,“}”(右括号)结束。每个“名称”后跟一个“:”(冒号);“‘名称/值’ 对”之间使用“,”(逗号)分隔。



数组是值(value)的有序集合。一个数组以“[”(左中括号)开始,“]”(右中括号)结束。值之间使用“,”(逗号)分隔。
对应函数: static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer)


值(value)可以是双引号括起来的字符串(string)、数值(number)、true、false、 null、对象(object)或者数组(array)。这些结构可以嵌套。

字符串(string)是由双引号包围的任意数量Unicode字符的集合,使用反斜线转义。一个字符(character)即一个单独的字符串(character string)。

字符串(string)与C或者Java的字符串非常相似。



数值(number)也与C或者Java的数值非常相似。除去未曾使用的八进制与十六进制格式。除去一些编码细节。

2.Cjson函数的使用

1. Cjson.c和Cjson.h两个文件
创建一个json数据
{
	"name": "Jack (\"Bee\") Nimble",
	"format": {
		"type": "rect",
		"width": 1920,
		"height": 1080,
		"he": 1,
		"interlace": false,
		"frame rate": 24
	}
}

1:创建一个json对象
cJSON *root1,*fmt;
root1=cJSON_CreateObject(); 
2:给对象添加键值
   
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笔记_第1张图片

一看时内存不够了:

cjson笔记_第2张图片

 
  
	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效果。

cjson笔记_第3张图片
cjson笔记_第4张图片


你可能感兴趣的:(cjson)