Files
ArduinoJson/extras/scripts/wandbox/JsonGeneratorExample.cpp

43 lines
973 B
C++
Raw Normal View History

// ArduinoJson - https://arduinojson.org
2025-02-24 15:18:26 +01:00
// Copyright © 2014-2025, Benoit BLANCHON
// 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
JsonDocument doc;
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;
2023-08-19 16:05:04 +02:00
// Add an array
JsonArray data = doc["data"].to<JsonArray>();
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:
// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
2019-02-26 10:02:52 +01:00
// Start a new line
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:
// {
// "sensor": "gps",
// "time": 1351824120,
// "data": [
// 48.756080,
// 2.302038
// ]
// }
}