JsonGeneratorExample.ino 935 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright Benoit Blanchon 2014
  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. StaticJsonBuffer<200> jsonBuffer;
  10. JsonArray& array = jsonBuffer.createArray();
  11. array.add(48.756080, 6); // 6 is the number of decimals to print
  12. array.add(2.302038, 6); // if not specified, 2 digits are printed
  13. JsonObject& root = jsonBuffer.createObject();
  14. root["sensor"] = "gps";
  15. root["time"] = 1351824120;
  16. root["data"] = array;
  17. root.printTo(Serial);
  18. // This prints:
  19. // {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
  20. Serial.println();
  21. root.prettyPrintTo(Serial);
  22. // This prints:
  23. // {
  24. // "sensor": "gps",
  25. // "time": 1351824120,
  26. // "data": [
  27. // 48.756080,
  28. // 2.302038
  29. // ]
  30. // }
  31. }
  32. void loop() {
  33. // not used in this example
  34. }