//ArduinoJson Assistant = https://arduinojson.org/v6/assistant/#/step1
#include <ArduinoJson.h>
void Deserialize();
void Serialize();
void setup() {
Serial.begin(9600);
Deserialize();
Serialize();
}
void loop() {
delay(1000L);
}
void Deserialize() {
String input = R"=====(
{
"sensor": "gps",
"time": 1351824120,
"data": [
48.75608,
2.302038
]
}
)=====";;
StaticJsonDocument<128> doc;
DeserializationError error = deserializeJson(doc, input);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
const char* sensor = doc["sensor"]; // "gps"
long time = doc["time"]; // 1351824120
float data_0 = doc["data"][0]; // 48.75608
float data_1 = doc["data"][1]; // 2.302038
Serial.println("Deserialize");
Serial.println("");
Serial.print("sensor = ");
Serial.print(sensor);
Serial.println("");
Serial.print("time = ");
Serial.print(time);
Serial.println("");
Serial.print("data_0 = ");
Serial.print(data_0);
Serial.println("");
Serial.print("data_1 = ");
Serial.print(data_1);
Serial.println("");
}
void Serialize() {
String output;
StaticJsonDocument<96> doc;
doc["sensor"] = "gps";
doc["time"] = 1351824120;
JsonArray data = doc.createNestedArray("data");
data.add(48.75608);
data.add(2.302038);
serializeJson(doc, output);
Serial.println("Serialize");
Serial.println("");
Serial.print("output = ");
Serial.print(output);
Serial.println("");
}