JsonGeneratorExample.ino 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2018
  3. // MIT License
  4. //
  5. // This example shows how to generate a JSON document with ArduinoJson.
  6. #include <ArduinoJson.h>
  7. void setup() {
  8. // Initialize Serial port
  9. Serial.begin(9600);
  10. while (!Serial) continue;
  11. // Allocate the JSON document
  12. //
  13. // Inside the brackets, 200 is the RAM allocated to this document.
  14. // Don't forget to change this value to match your requirement.
  15. // Use arduinojson.org/v6/assistant to compute the capacity.
  16. StaticJsonDocument<200> doc;
  17. // StaticJsonObject allocates memory on the stack, it can be
  18. // replaced by DynamicJsonDocument which allocates in the heap.
  19. //
  20. // DynamicJsonDocument doc(200);
  21. // Add values in the document
  22. //
  23. doc["sensor"] = "gps";
  24. doc["time"] = 1351824120;
  25. // Add an array.
  26. //
  27. JsonArray data = doc.createNestedArray("data");
  28. data.add(48.756080);
  29. data.add(2.302038);
  30. // Generate the minified JSON and send it to the Serial port.
  31. //
  32. serializeJson(doc, Serial);
  33. // The above line prints:
  34. // {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
  35. // Start a new line
  36. Serial.println();
  37. // Generate the prettified JSON and send it to the Serial port.
  38. //
  39. serializeJsonPretty(doc, Serial);
  40. // The above line prints:
  41. // {
  42. // "sensor": "gps",
  43. // "time": 1351824120,
  44. // "data": [
  45. // 48.756080,
  46. // 2.302038
  47. // ]
  48. // }
  49. }
  50. void loop() {
  51. // not used in this example
  52. }
  53. // Visit https://arduinojson.org/v6/example/generator/ for more.