JsonParserExample.ino 870 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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. char json[] =
  14. "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
  15. JsonObject& root = jsonBuffer.parseObject(json);
  16. if (!root.success()) {
  17. Serial.println("parseObject() failed");
  18. return;
  19. }
  20. const char* sensor = root["sensor"];
  21. long time = root["time"];
  22. double latitude = root["data"][0];
  23. double longitude = root["data"][1];
  24. Serial.println(sensor);
  25. Serial.println(time);
  26. Serial.println(latitude, 6);
  27. Serial.println(longitude, 6);
  28. }
  29. void loop() {
  30. // not used in this example
  31. }