JsonGeneratorExample.ino 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2025, Benoit BLANCHON
  3. // MIT License
  4. //
  5. // This example shows how to generate a JSON document with ArduinoJson.
  6. //
  7. // https://arduinojson.org/v7/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. JsonDocument doc;
  16. // Add values in the document
  17. doc["sensor"] = "gps";
  18. doc["time"] = 1351824120;
  19. // Add an array
  20. JsonArray data = doc["data"].to<JsonArray>();
  21. data.add(48.756080);
  22. data.add(2.302038);
  23. // Generate the minified JSON and send it to the Serial port
  24. serializeJson(doc, Serial);
  25. // The above line prints:
  26. // {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
  27. // Start a new line
  28. Serial.println();
  29. // Generate the prettified JSON and send it to the Serial port
  30. serializeJsonPretty(doc, Serial);
  31. // The above line prints:
  32. // {
  33. // "sensor": "gps",
  34. // "time": 1351824120,
  35. // "data": [
  36. // 48.756080,
  37. // 2.302038
  38. // ]
  39. // }
  40. }
  41. void loop() {
  42. // not used in this example
  43. }
  44. // See also
  45. // --------
  46. //
  47. // https://arduinojson.org/ contains the documentation for all the functions
  48. // used above. It also includes an FAQ that will help you solve any
  49. // serialization problem.
  50. //
  51. // The book "Mastering ArduinoJson" contains a tutorial on serialization.
  52. // It begins with a simple example, like the one above, and then adds more
  53. // features like serializing directly to a file or an HTTP request.
  54. // Learn more at https://arduinojson.org/book/
  55. // Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤