JsonGeneratorExample.ino 945 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright Benoit Blanchon 2014-2015
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://github.com/bblanchon/ArduinoJson
  6. #include <ArduinoJson.h>
  7. void setup() {
  8. Serial.begin(9600);
  9. while (!Serial) {
  10. // wait serial port initialization
  11. }
  12. StaticJsonBuffer<200> jsonBuffer;
  13. JsonObject& root = jsonBuffer.createObject();
  14. root["sensor"] = "gps";
  15. root["time"] = 1351824120;
  16. JsonArray& data = root.createNestedArray("data");
  17. data.add(double_with_n_digits(48.756080, 6));
  18. data.add(double_with_n_digits(2.302038, 6));
  19. root.printTo(Serial);
  20. // This prints:
  21. // {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
  22. Serial.println();
  23. root.prettyPrintTo(Serial);
  24. // This prints:
  25. // {
  26. // "sensor": "gps",
  27. // "time": 1351824120,
  28. // "data": [
  29. // 48.756080,
  30. // 2.302038
  31. // ]
  32. // }
  33. }
  34. void loop() {
  35. // not used in this example
  36. }