当前位置:网站首页>arduino JSON数据信息解析
arduino JSON数据信息解析
2022-07-06 09:16:00 【一入极客深似海】
参数考太极创客的程序代码,将代码风格换成我习惯的用法,并在注释上加上自己的理解
单一对象JSON解析
JSON格式
{
"errno":0,
"error":"succ"
}
程序代码
#include <ArduinoJson.h>
程序void setup() {
Serial.begin(9600);
Serial.println("");
// 重点1:即将解析的json文件
String json ="{\"errno\":0,\"data\":{\"update_at\":\"2022-06-11 10:48:15\",\"id\":\"TEM\",\"create_time\":\"2022-06-10 20:15:36\",\"current_value\":45},\"error\":\"succ\"}";
Serial.println(String("")+"JSON大小:"+json.length());
// 重点2:解析的JSON数据大小
DynamicJsonDocument doc(json.length()*2); //解析的JSON数据大小
// 重点3:反序列化数据
deserializeJson(doc, json);
// 重点4:获取解析后的数据信息
String nameStr = doc["update_at"].as<String>();
int numberInt = doc["errno"].as<int>();
// 通过串口监视器输出解析后的数据信息
Serial.print("errorStr = ");Serial.println(nameStr);
Serial.print("errnoInt = ");Serial.println(numberInt);
}
void loop() {
}
进阶一点,JSON是这种形式的
{
"errno":0,
"data":{
"update_at":"2022-06-11 10:48:15",
"id":"TEM",
"create_time":"2022-06-10 20:15:36",
"current_value":45
},
"error":"succ"
}
我们需要提取【data】->【update_at】或者【id】的内容
#include <ArduinoJson.h>
void setup() {
Serial.begin(9600);
Serial.println("");
// 重点1:即将解析的json文件
String json ="{\"errno\":0,\"data\":{\"update_at\":\"2022-06-11 10:48:15\",\"id\":\"TEM\",\"create_time\":\"2022-06-10 20:15:36\",\"current_value\":45},\"error\":\"succ\"}";
Serial.println(String("")+"JSON大小:"+json.length());
// 重点1:解析的JSON数据大小
DynamicJsonDocument doc(json.length()*2); //解析的JSON数据大小
// 重点3:反序列化数据
deserializeJson(doc, json);
// 重点4:获取解析后的数据信息
String nameStr = doc["data"]["create_time"].as<String>();
int numberInt = doc["errno"].as<int>();
// 通过串口监视器输出解析后的数据信息
Serial.print("errorStr = ");Serial.println(nameStr);
Serial.print("errnoInt = ");Serial.println(numberInt);
}
void loop() {
}
串口打印输出结果
解析数组
数组的形式我这里介绍两种比较复杂的,基本上者两种复杂的看明白,简单的就很清楚怎么修改了。最简单的基础,我建议大家去太极创客上去,人家讲的比我好
形式一
{
"results": [
{
"location": {
"name": "Beijing",
"country": "CN"
},
"now": {
"text": "Clear",
"code": "1",
"temperature": "3"
},
"last_update": "2020-03-01T20:10:00+08:00"
}
]
}
要取出【results】->【location】->【name】和【now】->【text】->【text】的值以及【last_update】的值
代码
#include <ArduinoJson.h>
#define json "{\"results\":[{\"location\":{\"name\":\"Beijing\",\"country\":\"CN\"},\"now\":{\"text\":\"Clear\",\"code\":\"1\",\"temperature\":\"3\"},\"last_update\":\"2020-03-01T20:10:00+08:00\"}]}"
void setup() {
Serial.begin(9600);
//重点1:计算要解析的json数据字节大小,数值的大小,要留有一定的缓冲余量,否则解析失败。返回null。就要注意是否字机大小没有给够。
//本次示例的这个JSON数据的大小,我给了双倍。一般来说JSON数据比较小给双倍,有些特别长的比如超过450 可以直接使用JSON本身的字节大小
int Strnum = strlen(json);
Serial.println(String("") + "json数据大小:" + Strnum);
DynamicJsonDocument doc(Strnum * 2); //
//重点2:反序列化数据
deserializeJson(doc, json);
//重点3:解析需要取出数组中的内容,这里取出数组中的第二位
JsonObject results_0 = doc["results"][0];
String create_time = results_0["location"]["name"].as<String>();
String create_time1 = results_0["now"]["text"].as<String>();
String create_time2 = results_0["last_update"].as<String>();
Serial.println(create_time);
Serial.println(create_time1);
Serial.println(create_time2);
}
void loop() {
}
形式二
{
"errno":22,
"data":[
{
"create_time":"2022-06-10 20:15:36",
"update_at":"2022-06-12 08:18:23",
"id":"TEM",
"uuid":"67b6c2b7-2ef9-4423-8170-f8a6ae0abf38",
"current_value":54
},
{
"create_time":"2022-06-10 20:15:36",
"update_at":"2022-06-12 08:18:23",
"id":"SoilHUM",
"uuid":"39630d74-e55a-4b64-a182-fe3556ad2746",
"current_value":17
},
{
"create_time":"2022-06-10 20:27:28",
"update_at":"2022-06-12 08:18:23",
"id":"hum2",
"uuid":"8a407e17-d1a0-4ff6-94cf-47229683b512",
"current_value":83
}
],
"error":"succ"
}
要取出其中数组中第二位【create_time】和【current_value】以及非数组中的【errno】和【error】
代码
#include <ArduinoJson.h>
#define json "{\"errno\":22,\"data\":[{\"create_time\":\"2022-06-10 20:15:36\",\"update_at\":\"2022-06-12 08:18:23\",\"id\":\"TEM\",\"uuid\":\"67b6c2b7-2ef9-4423-8170-f8a6ae0abf38\",\"current_value\":54},{\"create_time\":\"2022-06-10 20:15:36\",\"update_at\":\"2022-06-12 08:18:23\",\"id\":\"SoilHUM\",\"uuid\":\"39630d74-e55a-4b64-a182-fe3556ad2746\",\"current_value\":17},{\"create_time\":\"2022-06-10 20:27:28\",\"update_at\":\"2022-06-12 08:18:23\",\"id\":\"hum2\",\"uuid\":\"8a407e17-d1a0-4ff6-94cf-47229683b512\",\"current_value\":83}],\"error\":\"succ\"}"
void setup() {
Serial.begin(9600);
//重点1:计算要解析的json数据大小,这里的JSON字节大小已经超过450,我直接使用JSON本身的字节大小
int Strnum = strlen(json);
Serial.println(String("") +"json数据大小:"+ Strnum);
DynamicJsonDocument doc(Strnum);
//重点2:反序列化数据
deserializeJson(doc, json);
//重点3:解析需要取出数组中的内容,这里取出数组中的第二位
JsonObject results_0 = doc["data"][1];
String create_time = results_0["create_time"].as<String>();
int location_name_String1 = results_0["current_value"].as<int>();
//重点4:这里取出非数组中的内容
int now_temperature_int = doc["errno"].as<int>();
String location_name_String2 = doc["error"].as<String>();
Serial.println(create_time);
Serial.println(location_name_String1);
Serial.println(location_name_String2);
Serial.println(now_temperature_int);
}
void loop() {
}
边栏推荐
- 2020 WANGDING cup_ Rosefinch formation_ Web_ nmap
- FTP文件上传文件实现,定时扫描文件夹上传指定格式文件文件到服务器,C语言实现FTP文件上传详解及代码案例实现
- Codeforces Round #771 (Div. 2)
- Gallery之图片浏览、组件学习
- Composition des mots (sous - total)
- Connexion sans mot de passe du noeud distribué
- Mall project -- day09 -- order module
- GNN的第一个简单案例:Cora分类
- 物联网系统框架学习
- [Blue Bridge Cup 2017 preliminary] grid division
猜你喜欢
随机推荐
Word排版(小计)
MongoDB
几个关于指针的声明【C语言】
Reading BMP file with C language
[Blue Bridge Cup 2017 preliminary] grid division
Redis面试题
[mrctf2020] dolls
Internet protocol details
MATLAB学习和实战 随手记
FreeRTOS 任务函数里面的死循环
When using lambda to pass parameters in a loop, the parameters are always the same value
yarn安装与使用
分布式节点免密登录
Wangeditor rich text component - copy available
L2-004 is this a binary search tree? (25 points)
常用正则表达式整理
SQL时间注入
Linux yum安装MySQL
高通&MTK&麒麟 手机平台USB3.0方案对比
ToggleButton实现一个开关灯的效果