STM32实用应用系列:Json数据格式 与 cJSON的使用

目录

一、Json的简介

1、Json的简介

2、Json的两种结构

二、cJSON的使用

1、cJSON 源码下载

2、Json的数据结构

3、cJSON 的使用示例

(1)生成Json对象

(2)Json数据的解析 与 Json数据的处理示例

(3)Json数组的添加与解析

(4)Json的释放


一、Json的简介

1、Json的简介

JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式。它基于 ECMAScript (欧洲计算机协会制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。

2、Json的两种结构

  • 对象:由大括号 { } 包含的 键值对(关键字 - 值)
{
    "key1" : value1,
    "key2": value2,
    .......
}
  • 数组:由中括号 [ ] 包含的 值 
[
    value1,
    value2,
    ......
]

关键字-key:  为字符串

值-value: 可以为 字符串、整形、true、false、null、对象或者数组.......

二、cJSON的使用

1、cJSON 源码下载

cJSON源码下载地址:https://github.com/DaveGamble/cJSON

2、Json的数据结构

  • cJson的数据结构

  •  其中cJson对象的类型有以下几种(int type 的值):
#define cJSON_Invalid (0)
#define cJSON_False  (1 << 0)
#define cJSON_True   (1 << 1)
#define cJSON_NULL   (1 << 2)
#define cJSON_Number (1 << 3)
#define cJSON_String (1 << 4)
#define cJSON_Array  (1 << 5)
#define cJSON_Object (1 << 6)
#define cJSON_Raw    (1 << 7) /* raw json */

3、cJSON 的使用示例

(1)生成Json对象

	cJSON * pJsonRoot = cJSON_CreateObject();
	cJSON * pJsonItem = cJSON_CreateObject();

	cJSON_AddStringToObject(pJsonItem, "LED", "on");
	cJSON_AddStringToObject(pJsonItem, "status", "normal");

	cJSON_AddItemToObject(pJsonRoot, "Item1", pJsonItem);

    char * pcJsonPrint = cJSON_PrintUnformatted(pJsonRoot);
	printf("pcJsonPrint :%s \r\n", pcJsonPrint);

打印出来的效果:

pcJsonPrint :{"Item1":{"LED":"on","status":"normal"}} 

(2)Json数据的解析 与 Json数据的处理示例

	/* Json数据的解析 */
	cJSON * pJsonParseItem[3] = {NULL};
	pJsonParseItem[0] = cJSON_Parse(pcJsonPrint);

	/* Json数据的处理 */
	if((pJsonParseItem[1] = cJSON_GetObjectItem(pJsonParseItem[0] , "Item1")) != NULL)
	{
		if((pJsonParseItem[2] = cJSON_GetObjectItem(pJsonParseItem[1], "LED")) != NULL)
		{
			if(strcasecmp(pJsonParseItem[2]->valuestring, "on") == 0)
			{
				printf("Led On \r\n");
			}
		}

		if((pJsonParseItem[2] = cJSON_GetObjectItem(pJsonParseItem[1], "status")) != NULL)
		{
			if(strcasecmp(pJsonParseItem[2]->valuestring, "normal") == 0)
			{
				printf("status normal \r\n");
			}
		}

	}

(3)Json数组的添加与解析

注意:cJson_Print 和 cJSON_PrintUnformatted 会向系统申请一段内存来保存串行化的数据,这里注意一定要手动释放其内存,不然会出现内存泄漏的情况。

	/* Json 数组的使用 */
	cJSON * pJsonRoot = cJSON_CreateObject();
	cJSON * pJsonItem = cJSON_CreateObject();

	cJSON * pJsonArray = cJSON_CreateArray();//创建数组对象 其中type 设置成cJSON_Array

	/* Json 数组添加值 */
	cJSON_AddStringToObject(pJsonItem, "LED", "on");
	cJSON_AddStringToObject(pJsonItem, "status", "normal");

	cJSON_AddItemToArray(pJsonArray, pJsonItem);//添加对象
	cJSON_AddNumberToObject(pJsonArray, "number", 1);//添加数值
	cJSON_AddStringToObject(pJsonArray, "str", "my string");//添加字符串


	cJSON_AddItemToObject(pJsonRoot, "array", pJsonArray);


	char * pcJsonPrint = cJSON_PrintUnformatted(pJsonRoot);
	printf("pcJsonPrint :%s \r\n", pcJsonPrint);

	/* Json数据的解析 */
	cJSON * pJsonParseItem[4] = {NULL};
	pJsonParseItem[0] = cJSON_Parse(pcJsonPrint);

	/* Json数据的处理 */
	if((pJsonParseItem[1] = cJSON_GetObjectItem(pJsonParseItem[0],"array")) != NULL)
	{
		uint8_t ucSize = cJSON_GetArraySize(pJsonParseItem[1]);
		for(uint8_t i = 0; i < ucSize; i++)
		{
			pJsonParseItem[2] = cJSON_GetArrayItem(pJsonParseItem[1], i);


			printf("pJsonParseItem[2]->type  :%d pJsonParseItem[2]->valuestring: %s \r\n", pJsonParseItem[2]->type, pJsonParseItem[2]->valuestring );
			if(pJsonParseItem[2]->type == cJSON_Object)
			{
				if((pJsonParseItem[3] = cJSON_GetObjectItem(pJsonParseItem[2], "led")) != NULL)
					printf("led:%s \r\n", pJsonParseItem[3]->valuestring);

				if((pJsonParseItem[3] = cJSON_GetObjectItem(pJsonParseItem[2], "status")) != NULL)
					printf("status:%s \r\n", pJsonParseItem[3]->valuestring);
			}

			if(pJsonParseItem[2]->type == cJSON_Number)
				printf("number:%d \r\n", pJsonParseItem[2]->valueint);

			if(pJsonParseItem[2]->type == cJSON_String)
				printf("str:%s\r\n", pJsonParseItem[2]->valuestring);

		}

	}

(4)Json的释放

cJSON_Delete(pJsonRoot);

注意:cJSON_Delete 会删除包括pJsonRoot在内及其包含的子对象。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不吃鱼的猫丿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值