JSON数据解析(遗忘备份)
代码主要是给自己看的,程序中有些特定的函数,均可以用C库中的函数替代。
参考链接
https://blog.csdn.net/pl0020/article/details/120277294
#include "iot_os.h"
#include "user_code.h"
#include "cjson.h"
/*
功能简介:解析DATA2 数据
2022-07-10
*/
char DATA2[]="{\"EntityNo\":\"140106000001\",\"DeviceNo\":\"00001\",\"Idx\":\"576934\",\"Func\":\"6\",\"Body\":[{\
\"Module\":\"ID1\",\"Status\":1,\"Reg_Val\":{\"Temp1\":8,\"Temp2\":7,\"Temp3\":9}}]}";
char buff[] = "JSON format ok, start parse!";
char buff1[] = "parse complete";
char buff2[] = "JSON format error";
char buff3[1024];
struct data_info{
unsigned char EntityNo[64]; //数据类型待考虑
unsigned char DeviceNo[64];
unsigned char Idx[64];
unsigned int Func;
unsigned char Module[100];
unsigned int Status;
unsigned int Reg_Val[4];
};
struct data_info data_info;
//解析数组
int parseData(uint8_t *str)
{
int ret = 0;
cJSON *root, *B_array; //创建根 (主根,数组根)
cJSON *Body, *Reg_Val; //数组名,数组中包含属性的对象名
root = cJSON_Parse((const char *)str); //创建JSON解析对象,返回JSON格式是否正确
if (root != 0)
{
zq_uart_write(buff,strlen(buff));
memset(data_info.DeviceNo,0x00,sizeof(data_info.DeviceNo)); //该赋值方式可以实现字符串解析读取
sprintf(data_info.DeviceNo,"%s",cJSON_GetObjectItem(root, "DeviceNo")->valuestring);
memset(data_info.EntityNo,0x00,sizeof(data_info.EntityNo)); //该赋值方式可以实现字符串解析读取
sprintf(data_info.EntityNo,"%s",cJSON_GetObjectItem(root, "EntityNo")->valuestring);
memset(data_info.Idx,0x00,sizeof(data_info.Idx)); //该赋值方式可以实现字符串解析读取
sprintf(data_info.Idx,"%s",cJSON_GetObjectItem(root, "Idx")->valuestring);
//通过调用cJSON_GetObjectItem,获取到数组results!!!!
B_array = cJSON_GetObjectItem(root, "Body"); //Body是服务器下发JSON数据的数组名
if(B_array->type == cJSON_Array)
{
//由于提前知道数组只包含1个元素,因此不用再调用cJSON_GetArraySize来获取元素个数,而是直接读第0个元素的值
Body = cJSON_GetArrayItem(B_array, 0);//在数组中检索第0个元素
if(Body->type == cJSON_Object)//如果第0个元素的类型是“对象”
{
/* Body data parse */ //将数组中的单属性对象的数据提取出来赋值给结构体变量
memset(data_info.Module,0x00,sizeof(data_info.Module)); //该赋值方式可以实现字符串解析读取
sprintf(data_info.Module,"%s",cJSON_GetObjectItem(Body, "Module")->valuestring);
data_info.Status = cJSON_GetObjectItem(Body, "Status")->valueint;
// /* Reg_Val data parse */
Reg_Val = cJSON_GetObjectItem(Body, "Reg_Val"); //获取数组中带多属性的对象
if(Reg_Val->type == cJSON_Object)
{
data_info.Reg_Val[0] = cJSON_GetObjectItem(Reg_Val, "Temp1")->valueint;
data_info.Reg_Val[1] = cJSON_GetObjectItem(Reg_Val, "Temp2")->valueint;
data_info.Reg_Val[2] = cJSON_GetObjectItem(Reg_Val, "Temp3")->valueint;
}
else return 1;
}
else return 1;
}
else return 1;
}
else return 1;
zq_uart_write(buff1,strlen(buff1));
cJSON_Delete(root);
return ret;
}
display()
{
unsigned char buff3[1024];
memset(buff3,0x00,sizeof(buff3));
sprintf(buff3,"EntityNo is %s, DeviceNo is %s, Idx is %s, Func is %d, Module is %s, Status is %d, Temp1 is %d, Temp2 is %d Temp3 is %d",\
data_info.EntityNo,data_info.DeviceNo,data_info.Idx,data_info.Func,data_info.Module,\
data_info.Status,data_info.Reg_Val[0],data_info.Reg_Val[1],data_info.Reg_Val[2]);
zq_uart_write(buff3,strlen(buff3));
}
void app_init(void)
{
//注册 服务器A 接收事件
//注册 串口 接收事件
struct zq_recv_cb cb;
memset(&cb,0x00,sizeof(cb));
zq_set_recv_cb(cb);
}
void app_main(PVOID pParameter)
{
app_init();
for(;;)
{
parseData(DATA2);
display();
zq_sleep(5000);
}
}
PTASK_MAIN user_task = app_main;
运行结果
EntityNo is 140106000001,
DeviceNo is 00001,
Idx is 576934,
Func is 6,
Module is ID1,
Status is 1,
Temp1 is 8,
Temp2 is 7
Temp3 is 9`