Files
ArduinoJson/examples/JsonParserExample/JsonParserExample.ino

81 lines
2.5 KiB
Arduino
Raw Normal View History

// ArduinoJson - arduinojson.org
2020-01-09 15:48:38 +01:00
// Copyright Benoit Blanchon 2014-2020
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.
//
// https://arduinojson.org/v6/example/parser/
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
//
// Inside the brackets, 200 is the capacity 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.
// Use arduinojson.org/v6/assistant to compute the capacity.
StaticJsonDocument<200> doc;
2014-03-01 12:59:45 +01:00
// StaticJsonDocument<N> allocates memory on the stack, it can be
// replaced by DynamicJsonDocument which allocates in the heap.
2016-02-01 13:31:07 +01:00
//
// DynamicJsonDocument doc(200);
2016-02-01 13:31:07 +01:00
// 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.
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
// Deserialize the JSON document
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) {
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
2016-02-01 13:31:07 +01:00
// 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];
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
// See also
// --------
//
// https://arduinojson.org/ contains the documentation for all the functions
// used above. It also includes an FAQ that will help you solve any
// deserialization problem.
//
// The book "Mastering ArduinoJson" contains a tutorial on deserialization.
// It begins with a simple example, like the one above, and then adds more
// features like deserializing directly from a file or an HTTP request.
// Learn more at https://arduinojson.org/book/
// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤