当前位置:网站首页>Arduino JSON data information parsing
Arduino JSON data information parsing
2022-07-06 12:04:00 【A geek is as deep as the sea】
Parameter test Taiji maker's program code , Replace the code style with my usual usage , And add your own understanding to the notes
A single object JSON analysis
JSON Format
{
"errno":0,
"error":"succ"
}
Program code
#include <ArduinoJson.h>
Program void setup() {
Serial.begin(9600);
Serial.println("");
// a key 1: To be resolved json file
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 size :"+json.length());
// a key 2: Analytic JSON data size
DynamicJsonDocument doc(json.length()*2); // Analytic JSON data size
// a key 3: Deserialized data
deserializeJson(doc, json);
// a key 4: Get the parsed data information
String nameStr = doc["update_at"].as<String>();
int numberInt = doc["errno"].as<int>();
// Output the parsed data information through the serial port monitor
Serial.print("errorStr = ");Serial.println(nameStr);
Serial.print("errnoInt = ");Serial.println(numberInt);
}
void loop() {
}
A little more advanced ,JSON It's in this form
{
"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"
}
We need to extract 【data】->【update_at】 perhaps 【id】 The content of
#include <ArduinoJson.h>
void setup() {
Serial.begin(9600);
Serial.println("");
// a key 1: To be resolved json file
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 size :"+json.length());
// a key 1: Analytic JSON data size
DynamicJsonDocument doc(json.length()*2); // Analytic JSON data size
// a key 3: Deserialized data
deserializeJson(doc, json);
// a key 4: Get the parsed data information
String nameStr = doc["data"]["create_time"].as<String>();
int numberInt = doc["errno"].as<int>();
// Output the parsed data information through the serial port monitor
Serial.print("errorStr = ");Serial.println(nameStr);
Serial.print("errnoInt = ");Serial.println(numberInt);
}
void loop() {
}
Serial port printout results
Parsing arrays
In the form of array, I will introduce two more complex , Basically, there are two complex ways to see , Simply, it's clear how to modify . The simplest Foundation , I suggest you go Taiji maker Up , Others speak better than me
Form 1
{
"results": [
{
"location": {
"name": "Beijing",
"country": "CN"
},
"now": {
"text": "Clear",
"code": "1",
"temperature": "3"
},
"last_update": "2020-03-01T20:10:00+08:00"
}
]
}
To remove 【results】->【location】->【name】 and 【now】->【text】->【text】 And 【last_update】 Value
Code
#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);
// a key 1: Calculate what to parse json Data byte size , The size of the value , There should be a certain buffer margin , Otherwise, the resolution fails . return null. We should pay attention to whether the size of the typewriter is not enough .
// This example JSON The size of the data , I gave double . Generally speaking JSON If the data is small, double it , Some are especially long, such as more than 450 You can use it directly JSON Byte size of itself
int Strnum = strlen(json);
Serial.println(String("") + "json data size :" + Strnum);
DynamicJsonDocument doc(Strnum * 2); //
// a key 2: Deserialized data
deserializeJson(doc, json);
// a key 3: Parsing requires fetching the contents of the array , Here, take the second bit in the array
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() {
}
Form 2
{
"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"
}
To get the second digit in the array 【create_time】 and 【current_value】 And... In non array 【errno】 and 【error】
Code
#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);
// a key 1: Calculate what to parse json data size , there JSON The byte size has exceeded 450, I use it directly JSON Byte size of itself
int Strnum = strlen(json);
Serial.println(String("") +"json data size :"+ Strnum);
DynamicJsonDocument doc(Strnum);
// a key 2: Deserialized data
deserializeJson(doc, json);
// a key 3: Parsing requires fetching the contents of the array , Here, take the second bit in the array
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>();
// a key 4: Here we take out the contents of the non array
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() {
}
边栏推荐
猜你喜欢
Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer
Togglebutton realizes the effect of switching lights
[template] KMP string matching
小天才电话手表 Z3工作原理
ES6语法总结--下篇(进阶篇 ES6~ES11)
共用体(union)详解【C语言】
电商数据分析--用户行为分析
Basic knowledge of lithium battery
Linux Yum install MySQL
Machine learning -- linear regression (sklearn)
随机推荐
Contiki source code + principle + function + programming + transplantation + drive + network (turn)
Distribute wxWidgets application
Gallery之图片浏览、组件学习
Priority inversion and deadlock
ESP learning problem record
Basic use of pytest
Vert. x: A simple TCP client and server demo
【ESP32学习-1】Arduino ESP32开发环境搭建
Gallery's image browsing and component learning
JS object and event learning notes
gcc 编译选项
荣耀Magic 3Pro 充电架构分析
Cannot change version of project facet Dynamic Web Module to 2.3.
C语言函数之可变参数原理:va_start、va_arg及va_end
JS array + array method reconstruction
Missing value filling in data analysis (focus on multiple interpolation method, miseforest)
Implementation scheme of distributed transaction
程序员老鸟都会搞错的问题 C语言基础 指针和数组
Principle and implementation of MySQL master-slave replication
使用LinkedHashMap实现一个LRU算法的缓存