Files
ArduinoJson/examples/JsonGeneratorExample/JsonGeneratorExample.ino

75 lines
2.1 KiB
Arduino
Raw Normal View History

// 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 generate a JSON document with ArduinoJson.
2014-11-03 21:29:55 +01:00
#include <ArduinoJson.h>
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-11-03 21:29:55 +01:00
// Root JSON object
2016-02-01 13:31:07 +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.
// Use arduinojson.org/assistant to compute the capacity.
StaticJsonObject<200> root;
2014-11-03 21:29:55 +01:00
// StaticJsonObject allocates memory on the stack, it can be
// replaced by DynamicJsonObject which allocates in the heap.
2016-02-01 13:31:07 +01:00
//
// DynamicJsonObject root(200);
2016-02-01 13:31:07 +01:00
// Add values in the object
//
// Most of the time, you can rely on the implicit casts.
// In other case, you can do root.set<long>("time", 1351824120);
2014-11-03 21:29:55 +01:00
root["sensor"] = "gps";
root["time"] = 1351824120;
2014-11-07 16:23:21 +01:00
2016-02-01 13:31:07 +01:00
// Add a nested array.
//
// It's also possible to create the array separately and add it to the
// JsonObject but it's less efficient.
2014-11-07 16:23:21 +01:00
JsonArray& data = root.createNestedArray("data");
data.add(48.756080);
data.add(2.302038);
2014-11-03 21:29:55 +01:00
root.printTo(Serial);
// This prints:
// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
Serial.println();
root.prettyPrintTo(Serial);
// This prints:
// {
// "sensor": "gps",
// "time": 1351824120,
// "data": [
// 48.756080,
// 2.302038
// ]
// }
}
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
// --------
//
// The website arduinojson.org contains the documentation for all the functions
// used above. It also includes an FAQ that will help you solve any
// serialization problem.
// Please check it out at: https://arduinojson.org/
//
// The book "Mastering ArduinoJson" contains a tutorial on serialization.
// It begins with a simple example, like the one above, and then adds more
// features like serializing directly to a file or an HTTP request.
// Please check it out at: https://arduinojson.org/book/