JsonGeneratorExample.ino 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. //
  5. // This example shows how to generate a JSON document with ArduinoJson.
  6. //
  7. // https://arduinojson.org/v6/example/generator/
  8. #include <ArduinoJson.h>
  9. void setup() {
  10. // Initialize Serial port
  11. Serial.begin(9600);
  12. while (!Serial) continue;
  13. // Allocate the JSON document
  14. //
  15. // Inside the brackets, 200 is the RAM allocated to this document.
  16. // Don't forget to change this value to match your requirement.
  17. // Use https://arduinojson.org/v6/assistant to compute the capacity.
  18. StaticJsonDocument<200> doc;
  19. // StaticJsonObject allocates memory on the stack, it can be
  20. // replaced by DynamicJsonDocument which allocates in the heap.
  21. //
  22. // DynamicJsonDocument doc(200);
  23. // Add values in the document
  24. //
  25. doc["sensor"] = "gps";
  26. doc["time"] = 1351824120;
  27. // Add an array.
  28. //
  29. JsonArray data = doc.createNestedArray("data");
  30. data.add(48.756080);
  31. data.add(2.302038);
  32. // Generate the minified JSON and send it to the Serial port.
  33. //
  34. serializeJson(doc, Serial);
  35. // The above line prints:
  36. // {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
  37. // Start a new line
  38. Serial.println();
  39. // Generate the prettified JSON and send it to the Serial port.
  40. //
  41. serializeJsonPretty(doc, Serial);
  42. // The above line prints:
  43. // {
  44. // "sensor": "gps",
  45. // "time": 1351824120,
  46. // "data": [
  47. // 48.756080,
  48. // 2.302038
  49. // ]
  50. // }
  51. }
  52. void loop() {
  53. // not used in this example
  54. }
  55. // See also
  56. // --------
  57. //
  58. // https://arduinojson.org/ contains the documentation for all the functions
  59. // used above. It also includes an FAQ that will help you solve any
  60. // serialization problem.
  61. //
  62. // The book "Mastering ArduinoJson" contains a tutorial on serialization.
  63. // It begins with a simple example, like the one above, and then adds more
  64. // features like serializing directly to a file or an HTTP request.
  65. // Learn more at https://arduinojson.org/book/
  66. // Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤