StringExample.ino 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  3. // MIT License
  4. //
  5. // This example shows the different ways you can use String with ArduinoJson.
  6. //
  7. // Use String objects sparingly, because ArduinoJson duplicates them in the
  8. // JsonDocument. Prefer plain old char[], as they are more efficient in term of
  9. // code size, speed, and memory usage.
  10. //
  11. // https://arduinojson.org/v6/example/string/
  12. #include <ArduinoJson.h>
  13. void setup() {
  14. DynamicJsonDocument doc(1024);
  15. // You can use a String as your JSON input.
  16. // WARNING: the string in the input will be duplicated in the JsonDocument.
  17. String input =
  18. "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
  19. deserializeJson(doc, input);
  20. JsonObject obj = doc.as<JsonObject>();
  21. // You can use a String to get an element of a JsonObject
  22. // No duplication is done.
  23. long time = obj[String("time")];
  24. // You can use a String to set an element of a JsonObject
  25. // WARNING: the content of the String will be duplicated in the JsonDocument.
  26. obj[String("time")] = time;
  27. // You can get a String from a JsonObject or JsonArray:
  28. // No duplication is done, at least not in the JsonDocument.
  29. String sensor = obj["sensor"];
  30. // Unfortunately, the following doesn't work (issue #118):
  31. // sensor = obj["sensor"]; // <- error "ambiguous overload for 'operator='"
  32. // As a workaround, you need to replace by:
  33. sensor = obj["sensor"].as<String>();
  34. // You can set a String to a JsonObject or JsonArray:
  35. // WARNING: the content of the String will be duplicated in the JsonDocument.
  36. obj["sensor"] = sensor;
  37. // It works with serialized() too:
  38. obj["sensor"] = serialized(sensor);
  39. // You can also concatenate strings
  40. // WARNING: the content of the String will be duplicated in the JsonDocument.
  41. obj[String("sen") + "sor"] = String("gp") + "s";
  42. // You can compare the content of a JsonObject with a String
  43. if (obj["sensor"] == sensor) {
  44. // ...
  45. }
  46. // Lastly, you can print the resulting JSON to a String
  47. String output;
  48. serializeJson(doc, output);
  49. }
  50. void loop() {
  51. // not used in this example
  52. }
  53. // See also
  54. // --------
  55. //
  56. // https://arduinojson.org/ contains the documentation for all the functions
  57. // used above. It also includes an FAQ that will help you solve any problem.
  58. //
  59. // The book "Mastering ArduinoJson" contains a quick C++ course that explains
  60. // how your microcontroller stores strings in memory. On several occasions, it
  61. // shows how you can avoid String in your program.
  62. // Learn more at https://arduinojson.org/book/
  63. // Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤