JsonGeneratorExample.ino 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. //
  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)
  13. continue;
  14. // Allocate the JSON document
  15. //
  16. // Inside the parentheses, 200 is the RAM allocated to this document.
  17. // Don't forget to change this value to match your requirement.
  18. // Use https://arduinojson.org/v6/assistant to compute the capacity.
  19. JsonDocument doc(200);
  20. // Add values in the document
  21. //
  22. doc["sensor"] = "gps";
  23. doc["time"] = 1351824120;
  24. // Add an array.
  25. //
  26. JsonArray data = doc.createNestedArray("data");
  27. data.add(48.756080);
  28. data.add(2.302038);
  29. // Generate the minified JSON and send it to the Serial port.
  30. //
  31. serializeJson(doc, Serial);
  32. // The above line prints:
  33. // {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
  34. // Start a new line
  35. Serial.println();
  36. // Generate the prettified JSON and send it to the Serial port.
  37. //
  38. serializeJsonPretty(doc, Serial);
  39. // The above line prints:
  40. // {
  41. // "sensor": "gps",
  42. // "time": 1351824120,
  43. // "data": [
  44. // 48.756080,
  45. // 2.302038
  46. // ]
  47. // }
  48. }
  49. void loop() {
  50. // not used in this example
  51. }
  52. // See also
  53. // --------
  54. //
  55. // https://arduinojson.org/ contains the documentation for all the functions
  56. // used above. It also includes an FAQ that will help you solve any
  57. // serialization problem.
  58. //
  59. // The book "Mastering ArduinoJson" contains a tutorial on serialization.
  60. // It begins with a simple example, like the one above, and then adds more
  61. // features like serializing directly to a file or an HTTP request.
  62. // Learn more at https://arduinojson.org/book/
  63. // Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤