2017-11-07 20:42:50 +01:00
|
|
|
// ArduinoJson - arduinojson.org
|
2018-01-05 09:20:01 +01:00
|
|
|
// Copyright Benoit Blanchon 2014-2018
|
2014-11-03 21:29:55 +01:00
|
|
|
// MIT License
|
2017-12-11 15:19:28 +01:00
|
|
|
//
|
|
|
|
// This example shows how to deserialize a JSON document with ArduinoJson.
|
2014-03-01 12:59:45 +01:00
|
|
|
|
2014-11-03 21:29:55 +01:00
|
|
|
#include <ArduinoJson.h>
|
2014-03-01 12:59:45 +01:00
|
|
|
|
2014-11-03 21:29:55 +01:00
|
|
|
void setup() {
|
2017-12-11 15:19:28 +01:00
|
|
|
// Initialize serial port
|
2014-11-03 21:29:55 +01:00
|
|
|
Serial.begin(9600);
|
2017-12-11 15:19:28 +01:00
|
|
|
while (!Serial) continue;
|
2014-07-03 14:00:51 +02:00
|
|
|
|
2018-06-07 11:21:54 +02:00
|
|
|
// Allocate the JSON document
|
2016-02-01 13:31:07 +01:00
|
|
|
//
|
2018-02-26 16:05:16 +01:00
|
|
|
// Inside the brackets, 200 is the size of the memory pool in bytes.
|
2017-12-11 15:19:28 +01:00
|
|
|
// Don't forget to change this value to match your JSON document.
|
2017-12-11 17:26:50 +01:00
|
|
|
// Use arduinojson.org/assistant to compute the capacity.
|
2018-04-17 21:27:45 +02:00
|
|
|
StaticJsonDocument<200> doc;
|
2014-03-01 12:59:45 +01:00
|
|
|
|
2018-04-17 21:27:45 +02:00
|
|
|
// StaticJsonDocument<N> allocates memory on the stack, it can be
|
2018-10-25 20:21:23 +08:00
|
|
|
// replaced by DynamicJsonDocument which allocates in the heap.
|
2016-02-01 13:31:07 +01:00
|
|
|
//
|
2018-10-25 20:21:23 +08:00
|
|
|
// DynamicJsonDocument doc(200);
|
2016-02-01 13:31:07 +01:00
|
|
|
|
|
|
|
// JSON input string.
|
|
|
|
//
|
|
|
|
// It's better to use a char[] as shown here.
|
|
|
|
// If you use a const char* or a String, ArduinoJson will
|
|
|
|
// have to make a copy of the input in the JsonBuffer.
|
2014-11-03 21:29:55 +01:00
|
|
|
char json[] =
|
|
|
|
"{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
|
2014-03-01 12:59:45 +01:00
|
|
|
|
2018-04-17 21:27:45 +02:00
|
|
|
// Deserialize the JSON document
|
2018-05-15 18:23:09 +02:00
|
|
|
DeserializationError error = deserializeJson(doc, json);
|
2014-03-01 12:59:45 +01:00
|
|
|
|
2016-02-01 13:31:07 +01:00
|
|
|
// Test if parsing succeeds.
|
2018-03-09 16:58:01 +01:00
|
|
|
if (error) {
|
2018-04-17 21:27:45 +02:00
|
|
|
Serial.print(F("deserializeJson() failed: "));
|
|
|
|
Serial.println(error.c_str());
|
2014-11-03 21:29:55 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-03-01 12:59:45 +01:00
|
|
|
|
2018-04-17 21:27:45 +02:00
|
|
|
// Get the root object in the document
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonObject root = doc.as<JsonObject>();
|
2018-04-17 21:27:45 +02:00
|
|
|
|
2016-02-01 13:31:07 +01:00
|
|
|
// Fetch values.
|
|
|
|
//
|
|
|
|
// Most of the time, you can rely on the implicit casts.
|
2018-06-07 11:21:54 +02:00
|
|
|
// In other case, you can do root["time"].as<long>();
|
2014-11-03 21:29:55 +01:00
|
|
|
const char* sensor = root["sensor"];
|
|
|
|
long time = root["time"];
|
|
|
|
double latitude = root["data"][0];
|
|
|
|
double longitude = root["data"][1];
|
2014-03-01 12:59:45 +01:00
|
|
|
|
2016-02-01 13:31:07 +01:00
|
|
|
// Print values.
|
2014-11-03 21:29:55 +01:00
|
|
|
Serial.println(sensor);
|
|
|
|
Serial.println(time);
|
|
|
|
Serial.println(latitude, 6);
|
|
|
|
Serial.println(longitude, 6);
|
2014-03-01 12:59:45 +01:00
|
|
|
}
|
|
|
|
|
2014-11-03 21:29:55 +01:00
|
|
|
void loop() {
|
|
|
|
// not used in this example
|
2015-08-20 15:15:59 +02:00
|
|
|
}
|
2017-12-11 15:19:28 +01:00
|
|
|
|
2018-07-02 09:35:21 +02:00
|
|
|
// Visit https://arduinojson.org/v6/example/parser/ for more.
|