JsonParserExample.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2025, Benoit BLANCHON
  3. // MIT License
  4. //
  5. // This example shows how to deserialize a JSON document with ArduinoJson.
  6. #include <iostream>
  7. #include "ArduinoJson.h"
  8. int main() {
  9. // Allocate the JSON document
  10. JsonDocument doc;
  11. // JSON input string
  12. const char* json =
  13. "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
  14. // Deserialize the JSON document
  15. DeserializationError error = deserializeJson(doc, json);
  16. // Test if parsing succeeds
  17. if (error) {
  18. std::cerr << "deserializeJson() failed: " << error.c_str() << std::endl;
  19. return 1;
  20. }
  21. // Fetch the values
  22. //
  23. // Most of the time, you can rely on the implicit casts.
  24. // In other case, you can do doc["time"].as<long>();
  25. const char* sensor = doc["sensor"];
  26. long time = doc["time"];
  27. double latitude = doc["data"][0];
  28. double longitude = doc["data"][1];
  29. // Print the values
  30. std::cout << sensor << std::endl;
  31. std::cout << time << std::endl;
  32. std::cout << latitude << std::endl;
  33. std::cout << longitude << std::endl;
  34. return 0;
  35. }