[Taiji maker esp8266 self study notes] data exchange format: JOSN

Article catalog

Article catalog

preface

2, Composition

1. Basic elements

3, JSON parsing

summary

reference resources

preface

Recent internship, smart home related, need to be developed with esp8266. I haven't studied 8266 before, b found on the station The related tutorials of [Tai Chi maker esp8266] are much simpler than 32 after a few days of study. You can get started quickly and don't need to call the very low-level things. However, you have a poor memory. You're afraid you'll always forget, so you just plan to sum up after watching the video, and then tap the knowledge points to deepen your memory. It's also convenient for you to come back and have a look if you forget. Moreover, Tai Chi maker The tutorial is about Zhendi fine [thank you for Taiji maker's free teaching video]

JSON saw it before learning crawlers, but it only knew what it was like txt, what it was like, it was a format, and after that, the package was finished, but what was it, it didn't know. At that time, I was in a fog when I arrived here. Now I can see the sun after learning this!

1, What is JSON?

JSON(JavaScript Object Notation) means JavaScript Object notation. Although the name has JS, it does not mean that it is only JS oriented. It just follows JS syntax and can be embedded in any language. Is a very common data exchange format.

Compared with XML, it has fewer anti-human tags and good readability. Compared with Ginkgo biloba (FOG), the founder claims that it will never be upgraded, which shows that json has long-term stability.

2, Composition

1. Basic elements

  • Data is rendered in name value pairs
  • Data names and values are separated by colons
  • Braces {} are used to label the contents of objects
  • Brackets [] are used to label the contents of the array
  • Commas are used to separate data, objects, and arrays

"Data name": JSON data value
    "Year":2016
    "URL":"www.xxx.com"

Colon: the following value can be {} or []

An object cannot contain {object} and [array] directly

An array can contain {object} and [array], but cannot directly store data ("data name": JOSN data value)

(relevant examples, from Taiji maker's official website):

{
  "results": [
    {
      "location": {
        "name": "Beijing",
        "country": "CN"
      },
      "now": {
        "text": "Clear",
        "code": "1",
        "temperature": "3"
      },
      "last_update": "2020-03-01T20:10:00+08:00"
    }
  ]
}

3, JSON parsing

Arduonjson Library under Arduino, parsing process:

1. JSON parsing for single object:

/**********************************************************************
Project name / Project: Internet of things for zero foundation introduction
 Program name / Program Name: arduinojosn_ 1_ object
 Team: Taiji maker team / Taichi maker (www.taichi-maker. Com)
Author: Cyno Shuo
 Date (YYYYMMDD): 20200424
 Program Purpose: 
This program demonstrates how to use the Arduino json library to parse the following json information. The json contains an object,
There is a data in the object.
{
  "name": "taichi-maker",
  "number": 1
}
-----------------------------------------------------------------------
This sample program is the sample program in the Internet of things for zero foundation introduction made by Taiji maker team.
This tutorial is designed and produced by friends who are interested in the development of the Internet of things. For more information about this tutorial, please refer to the following web pages:
http://www.taichi-maker.com/homepage/esp8266-nodemcu-iot/
***********************************************************************/
#include <ArduinoJson.h>
 
void setup() {
  Serial.begin(9600);
  Serial.println("");
 
  // Key 1: DynamicJsonDocument object
  const size_t capacity = JSON_OBJECT_SIZE(2) + 30;
  DynamicJsonDocument doc(capacity);
 
  // Key 2: json file to be parsed
  String json = "{\"name\":\"taichi-maker\",\"number\":1}";
  
  // Key 3: deserializing data
  deserializeJson(doc, json);
 
  // Key 4: obtain the parsed data information
  String nameStr = doc["name"].as<String>();
  int numberInt = doc["number"].as<int>();
 
  // Output the parsed data information through the serial port monitor
  Serial.print("nameStr = ");Serial.println(nameStr);
  Serial.print("numberInt = ");Serial.println(numberInt);
}
 
void loop() {}

2. Parsing array JSON

/**********************************************************************
Project name / Project: Internet of things for zero foundation introduction
 Program name / Program Name: arduinojosn_ 2_ array
 Team: Taiji maker team / Taichi maker (www.taichi-maker. Com)
Author: Cyno Shuo
 Date (YYYYMMDD): 20200424
 Program Purpose: 
This program is used to demonstrate how to use the Arduino json library to parse the following json information, which contains an array,
The array has two elements, each element is an object, and each object has a data.
[
  {
    "name": "taichi-maker"
  },
  {
    "website": "www.taichi-maker.com"
  }
]
-----------------------------------------------------------------------
This sample program is the sample program in the Internet of things for zero foundation introduction made by Taiji maker team.
This tutorial is designed and produced by friends who are interested in the development of the Internet of things. For more information about this tutorial, please refer to the following web pages:
http://www.taichi-maker.com/homepage/esp8266-nodemcu-iot/
***********************************************************************/
#include <ArduinoJson.h>
 
void setup() {
  Serial.begin(9600);
 
  // Key 1: DynamicJsonDocument object
  const size_t capacity = JSON_ARRAY_SIZE(2) + 2*JSON_OBJECT_SIZE(1) + 60;
  DynamicJsonDocument doc(capacity);
  
  // Key 2: json file to be parsed
  String json = "[{\"name\":\"taichi-maker\"},{\"website\":\"www.taichi-maker.com\"}]";
 
  // Key 3: deserializing data
  deserializeJson(doc, json);
 
 
  String nameStr = doc[0]["name"].as<String>();
  String websiteStr = doc[1]["website"].as<String>();
 
  // Output the parsed data information through the serial port monitor
  Serial.print("nameStr = ");Serial.println(nameStr);
  Serial.print("websiteStr = ");Serial.println(websiteStr);
}
 
void loop() {}

Basic steps: 1 Set the dynamic json object and set the space size

Where: JSON_ARRAY_SIZE(n)    JSON_OBJECT_SIZE(n) indicates the corresponding size of} array and object, and the number added after it provides additional space for JSON parsing

The calculation capacity can be directly provided by using the online tools on the official website of ArduinoJson

                  2. Put in the json file to be parsed

                  3. Parsing json data

                  4. The parsed data is put into variables and then output

0 in doc[0] represents the 0th value in the data

summary

Basically, this is the basic usage of arduonjson library for json. Later, two esp8266 are used to transfer json data to each other. It is not used yet. When it is used later, come back and write some summaries

reference resources

Taiji maker official website

Basic use of josn

Keywords: IoT

Added by rolajaz on Fri, 31 Dec 2021 10:03:22 +0200