1. Basics
JSON(JavaScript Object Notation) is a general-purpose lightweight data interchange text format. It is easy for humans to read and write, and easy for machines to parse and generate. It uses JavaScript syntax to store and describe data objects, but JSON is completely independent of JavaScript. JSON is available for many popular programming languages. These properties make JSON an ideal data exchange format.
1. Important concepts of JSON: data, objects, arrays
2. Main points of JSON grammar rules:
- Data is presented as "name" "value" pairs
- The data "name" and "value" are separated by a colon
- Braces {} are used to mark the content of the object
- Brackets [] are used to mark the contents of the array
- Commas are used to separate data, objects, arrays
3.JSON data: JSON data name needs to be placed in double quotes "JSON data name": JSON data value
E.g:
"Year": 2016
"URL":"www.taichi-maker.com"
4.JSON data value:
- number (integer or float)
- string
- logical value (true or false)
- array (in square brackets)
- object (in braces)
- null
Note: A JSON data name can only correspond to one value.
Example of JSON numeric data: "value" : 25
Example of JSON string data: "name" : "taichi-maker"
Example of JSON logical value data: "bool_value" : true
Example of JSON array data:
"info": [ { "name" : "taichi-maker", "website" : "www.taichi-maker.com" }, { "year": 2020, "month": 12, "day": 30 } ]
Example of JSON object data:
"info": { "name" : "taichi-maker", "website" : "www.taichi-maker.com" }
Example of a JSON object with multiple data:
{ "info": { "name": "taichi-maker", "website": "www.taichi-maker.com" }, "date": { "year": 2020, "month": 12, "day": 30 } }
Note: Objects cannot store objects directly, nor can they store arrays directly.
Summarize:
(1) Meaning of symbols:
: used to separate the "name" and "value" of the data
{} annotation object content
[] Annotate the content of the array
, to separate data, objects, and arrays
2. Use ESP32 to parse JSON data (important)
size_t is unsigned int
#include <Arduino.h> #include <ArduinoJson.h> void setup() { Serial.begin(115200); Serial.println(""); // Key 1: DynamicJsonDocument object const size_t capacity = JSON_OBJECT_SIZE(2) + 30; DynamicJsonDocument doc(capacity); // Key point 2: The json file to be parsed String json = "{\"name\":\"taichi-maker\",\"number\":1}"; // Key 3: Deserialize data deserializeJson(doc, json); // Key 4: Get the parsed data information String nameStr = doc["name"].as<String>(); int numberInt = doc["number"].as<int>(); // Output the parsed data information through the serial monitor Serial.print("nameStr = ");Serial.println(nameStr); Serial.print("numberInt = ");Serial.println(numberInt); } void loop() {}
Key point 1: const size_t capacity = JSON_OBJECT_SIZE(2) + 30;
DynamicJsonDocument doc(capacity);
Here we create a DynamicJsonDocument object named doc. When creating the object, you need to provide a parameter, which is the parameter capacity in parentheses. The function of this capacity parameter is to tell ESP32 how much memory space the DynamicJsonDocument object we created will occupy. +30 These extra values are due to the fact that the ArduinoJson library needs extra space to copy the JSON message when parsing the message.
The JSON_OBJECT_SIZE(2) directive is used. The 2 in the instruction brackets means that the object contains two data.
Key point 2: String json = "{\"name\":\"taichi-maker\",\"number\":1}";
The function of this statement is to create a string variable and change it to store the JSON information that needs to be parsed. "/" is the format requirement
Key point 3: deserializeJson(doc, json); The purpose of this part of the statement is to use deserializeJson to parse the JSON file.
Key 4: String nameStr = doc["name"].as();
int numberInt = doc["number"].as();
These two statements are used to obtain the parsed JSON information, where doc["name"].as will return the value of "name". The .as in this statement will return the value of "name" as a string.
refer to: MQTT online parsing library Assistant | ArduinoJson 6
3-4-4-2 ESP8266 JSON Parsing – Taichi Maker (taichi-maker.com)
Example: {"msg":"OFF"}
Parse:
StaticJsonDocument<48> doc; DeserializationError error = deserializeJson(doc, input); if (error) { Serial.print("deserializeJson() failed: "); Serial.println(error.c_str()); return; } const char* msg = doc["msg"]; // "OFF"
Example 2:
/*JSON data analysis*/ StaticJsonDocument<32> doc; DeserializationError error = deserializeJson(doc, data); if (error) { Serial.print("deserializeJson() failed: "); Serial.println(error.c_str()); return; } int msg = doc["msg"]; // 1 Serial.print("msg = "); Serial.println(msg); /*Perform corresponding actions based on JSON information*/ switch (msg) { case 1: Serial.println("LED_on"); break; case 2: Serial.println("LED_off"); break; default: break; }