一、库文件 ArduinoJSON
可以使用 ArduinoJSON库 来解析和处理JSON数据。
二、JSON数据 序列化 Serialization
序列化(serialization):
序列化是将数据结构或对象状态转换为可存储或传输的格式。
测试代码:
#include <ArduinoJson.h>void setup() {// Initialize Serial portSerial.begin(115200);while (!Serial) continue;Serial.println();// Allocate the JSON document//// Inside the brackets, 200 is the RAM allocated to this document.// Don't forget to change this value to match your requirement.// Use arduinojson.org/v6/assistant to compute the capacity.StaticJsonDocument<200> doc;// StaticJsonObject allocates memory on the stack, it can be// replaced by DynamicJsonDocument which allocates in the heap.//// DynamicJsonDocument doc(200);// Add values in the document//doc["sensor"] = "gps";doc["time"] = 1351824120;// Add an array.//JsonArray data = doc.createNestedArray("data");data.add(48.756080);data.add(2.302038);// Generate the minified JSON and send it to the Serial port.//serializeJson(doc, Serial);// The above line prints:// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}// Start a new lineSerial.println();// Generate the prettified JSON and send it to the Serial port.//serializeJsonPretty(doc, Serial);// The above line prints:// {// "sensor": "gps",// "time": 1351824120,// "data": [// 48.756080,// 2.302038// ]// }
}void loop() {// not used in this example
}
运行效果如下:
三、JSON数据 反序列化 Deserialization
反序列化(deserialization):
在编程中,反序列化是将数据结构或对象状态从存储格式(如JSON、XML等)转换回程序能够使用的数据结构或对象实例的过程。
测试代码:
#include <ArduinoJson.h>void setup() {// Initialize serial portSerial.begin(9600);while (!Serial) continue;Serial.println();// Allocate the JSON document//// Inside the brackets, 200 is the capacity of the memory pool in bytes.// Don't forget to change this value to match your JSON document.// Use arduinojson.org/v6/assistant to compute the capacity.StaticJsonDocument<200> doc;// StaticJsonDocument<N> allocates memory on the stack, it can be// replaced by DynamicJsonDocument which allocates in the heap.//// DynamicJsonDocument doc(200);// JSON input string.//// Using a char[], as shown here, enables the "zero-copy" mode. This mode uses// the minimal amount of memory because the JsonDocument stores pointers to// the input buffer.// If you use another type of input, ArduinoJson must copy the strings from// the input to the JsonDocument, so you need to increase the capacity of the// JsonDocument.char json[] ="{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";// Deserialize the JSON documentDeserializationError error = deserializeJson(doc, json);// Test if parsing succeeds.if (error) {Serial.print(F("deserializeJson() failed: "));Serial.println(error.f_str());return;}// Fetch values.//// Most of the time, you can rely on the implicit casts.// In other case, you can do doc["time"].as<long>();const char* sensor = doc["sensor"];long time = doc["time"];double latitude = doc["data"][0];double longitude = doc["data"][1];// Print values.Serial.println(sensor);Serial.println(time);Serial.println(latitude, 6);Serial.println(longitude, 6);
}void loop() {// not used in this example
}
运行结果: