Files
ArduinoJson/examples/JsonGeneratorExample/JsonGeneratorExample.ino

65 lines
1.6 KiB
Arduino
Raw Normal View History

// ArduinoJson - arduinojson.org
2019-02-15 13:31:46 +01:00
// Copyright Benoit Blanchon 2014-2019
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
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 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;
2014-11-03 21:29:55 +01:00
// StaticJsonObject 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);
// Add values in the document
2016-02-01 13:31:07 +01:00
//
doc["sensor"] = "gps";
doc["time"] = 1351824120;
2014-11-07 16:23:21 +01:00
// Add an array.
2016-02-01 13:31:07 +01:00
//
JsonArray data = doc.createNestedArray("data");
data.add(48.756080);
data.add(2.302038);
2014-11-03 21:29:55 +01:00
// Generate the minified JSON and send it to the Serial port.
//
serializeJson(doc, Serial);
// The above line prints:
2014-11-03 21:29:55 +01:00
// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
// Start a new line
2014-11-03 21:29:55 +01:00
Serial.println();
// Generate the prettified JSON and send it to the Serial port.
//
serializeJsonPretty(doc, Serial);
// The above line prints:
2014-11-03 21:29:55 +01:00
// {
// "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
// Visit https://arduinojson.org/v6/example/generator/ for more.