当前位置:网站首页>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() {
}
边栏推荐
- Oppo vooc fast charging circuit and protocol
- 2019 Tencent summer intern formal written examination
- Matlab learning and actual combat notes
- GCC compilation options
- Keyword inline (inline function) usage analysis [C language]
- Contiki源码+原理+功能+编程+移植+驱动+网络(转)
- Stage 4 MySQL database
- Distribute wxWidgets application
- Selective sorting and bubble sorting [C language]
- I2C总线时序详解
猜你喜欢
STM32 如何定位导致发生 hard fault 的代码段
荣耀Magic 3Pro 充电架构分析
Come and walk into the JVM
Comparaison des solutions pour la plate - forme mobile Qualcomm & MTK & Kirin USB 3.0
【ESP32学习-1】Arduino ESP32开发环境搭建
Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer
E-commerce data analysis -- User Behavior Analysis
I2C bus timing explanation
Machine learning -- linear regression (sklearn)
锂电池基础知识
随机推荐
优先级反转与死锁
Linux yum安装MySQL
MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解
Pytorch实现简单线性回归Demo
Kaggle competition two Sigma connect: rental listing inquiries
Kaggle竞赛-Two Sigma Connect: Rental Listing Inquiries
MySQL主从复制的原理以及实现
Basic knowledge of lithium battery
Togglebutton realizes the effect of switching lights
Cannot change version of project facet Dynamic Web Module to 2.3.
Machine learning -- decision tree (sklearn)
Pytoch temperature prediction
sklearn之feature_extraction.text.CountVectorizer / TfidVectorizer
Using LinkedHashMap to realize the caching of an LRU algorithm
Contiki源码+原理+功能+编程+移植+驱动+网络(转)
ToggleButton实现一个开关灯的效果
互聯網協議詳解
Amba, ahb, APB, Axi Understanding
Bubble sort [C language]
I2C总线时序详解