2021-03-29 17:14:01 +02:00
|
|
|
// ArduinoJson - https://arduinojson.org
|
2025-02-24 15:18:26 +01:00
|
|
|
// Copyright © 2014-2025, Benoit BLANCHON
|
2018-11-14 18:02:01 +01:00
|
|
|
// MIT License
|
|
|
|
//
|
|
|
|
// This example shows how to generate a JSON document with ArduinoJson.
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include "ArduinoJson.h"
|
|
|
|
|
|
|
|
int main() {
|
2019-02-26 10:02:52 +01:00
|
|
|
// Allocate the JSON document
|
2023-07-17 18:15:13 +02:00
|
|
|
JsonDocument doc;
|
2018-11-14 18:02:01 +01:00
|
|
|
|
2023-08-19 16:05:04 +02:00
|
|
|
// Add values in the document.
|
2019-02-26 10:02:52 +01:00
|
|
|
doc["sensor"] = "gps";
|
|
|
|
doc["time"] = 1351824120;
|
2018-11-14 18:02:01 +01:00
|
|
|
|
2023-08-19 16:05:04 +02:00
|
|
|
// Add an array
|
2023-08-09 11:31:29 +02:00
|
|
|
JsonArray data = doc["data"].to<JsonArray>();
|
2018-11-14 18:02:01 +01:00
|
|
|
data.add(48.756080);
|
|
|
|
data.add(2.302038);
|
|
|
|
|
2019-02-26 10:02:52 +01:00
|
|
|
// Generate the minified JSON and send it to STDOUT
|
|
|
|
serializeJson(doc, std::cout);
|
|
|
|
// The above line prints:
|
2018-11-14 18:02:01 +01:00
|
|
|
// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
|
|
|
|
|
2019-02-26 10:02:52 +01:00
|
|
|
// Start a new line
|
2018-11-14 18:02:01 +01:00
|
|
|
std::cout << std::endl;
|
|
|
|
|
2019-02-26 10:02:52 +01:00
|
|
|
// Generate the prettified JSON and send it to STDOUT
|
|
|
|
serializeJsonPretty(doc, std::cout);
|
|
|
|
// The above line prints:
|
2018-11-14 18:02:01 +01:00
|
|
|
// {
|
|
|
|
// "sensor": "gps",
|
|
|
|
// "time": 1351824120,
|
|
|
|
// "data": [
|
|
|
|
// 48.756080,
|
|
|
|
// 2.302038
|
|
|
|
// ]
|
|
|
|
// }
|
|
|
|
}
|