当前位置:网站首页>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
 Insert picture description here

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() {
    
}

 Insert picture description here

原网站

版权声明
本文为[A geek is as deep as the sea]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060913281341.html