JsonGeneratorExample.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2023, 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. //
  11. // Inside the parentheses, 200 is the RAM allocated to this document.
  12. // Don't forget to change this value to match your requirement.
  13. // Use https://arduinojson.org/v6/assistant to compute the capacity.
  14. JsonDocument doc(200);
  15. // Add values in the document
  16. //
  17. doc["sensor"] = "gps";
  18. doc["time"] = 1351824120;
  19. // Add an array.
  20. //
  21. JsonArray data = doc.createNestedArray("data");
  22. data.add(48.756080);
  23. data.add(2.302038);
  24. // Generate the minified JSON and send it to STDOUT
  25. //
  26. serializeJson(doc, std::cout);
  27. // The above line prints:
  28. // {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
  29. // Start a new line
  30. std::cout << std::endl;
  31. // Generate the prettified JSON and send it to STDOUT
  32. //
  33. serializeJsonPretty(doc, std::cout);
  34. // The above line prints:
  35. // {
  36. // "sensor": "gps",
  37. // "time": 1351824120,
  38. // "data": [
  39. // 48.756080,
  40. // 2.302038
  41. // ]
  42. // }
  43. }