StringExample.ino 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  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. // You can use a String as a key to get a member from JsonDocument
  21. // No duplication is done.
  22. long time = doc[String("time")];
  23. // You can use a String as a key to set a member of a JsonDocument
  24. // WARNING: the content of the String will be duplicated in the JsonDocument.
  25. doc[String("time")] = time;
  26. // You can get the content of a JsonVariant as a String
  27. // No duplication is done, at least not in the JsonDocument.
  28. String sensor = doc["sensor"];
  29. // Unfortunately, the following doesn't work (issue #118):
  30. // sensor = doc["sensor"]; // <- error "ambiguous overload for 'operator='"
  31. // As a workaround, you need to replace by:
  32. sensor = doc["sensor"].as<String>();
  33. // You can set a String as the content of a JsonVariant
  34. // WARNING: the content of the String will be duplicated in the JsonDocument.
  35. doc["sensor"] = sensor;
  36. // It works with serialized() too:
  37. doc["sensor"] = serialized(sensor);
  38. // You can also concatenate strings
  39. // WARNING: the content of the String will be duplicated in the JsonDocument.
  40. doc[String("sen") + "sor"] = String("gp") + "s";
  41. // You can compare the content of a JsonObject with a String
  42. if (doc["sensor"] == sensor) {
  43. // ...
  44. }
  45. // Lastly, you can print the resulting JSON to a String
  46. // WARNING: it doesn't replace the content but appends to it
  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 ❤❤❤❤❤