JsonGeneratorExample.cpp 973 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2024, Benoit BLANCHON
  3. // MIT License
  4. //
  5. // This example shows how to generate a JSON document with ArduinoJson.
  6. #include <iostream>
  7. #include "ArduinoJson.h"
  8. int main() {
  9. // Allocate the JSON document
  10. JsonDocument doc;
  11. // Add values in the document.
  12. doc["sensor"] = "gps";
  13. doc["time"] = 1351824120;
  14. // Add an array
  15. JsonArray data = doc["data"].to<JsonArray>();
  16. data.add(48.756080);
  17. data.add(2.302038);
  18. // Generate the minified JSON and send it to STDOUT
  19. serializeJson(doc, std::cout);
  20. // The above line prints:
  21. // {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
  22. // Start a new line
  23. std::cout << std::endl;
  24. // Generate the prettified JSON and send it to STDOUT
  25. serializeJsonPretty(doc, std::cout);
  26. // The above line prints:
  27. // {
  28. // "sensor": "gps",
  29. // "time": 1351824120,
  30. // "data": [
  31. // 48.756080,
  32. // 2.302038
  33. // ]
  34. // }
  35. }