| 1 | // ArduinoJson - https://arduinojson.org |
| 2 | // Copyright © 2014-2026, 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/v7/example/string/ |
| 12 | |
| 13 | #include <ArduinoJson.h> |
| 14 | |
| 15 | void setup() { |
| 16 | JsonDocument doc; |
| 17 | |
| 18 | // You can use a String as your JSON input. |
| 19 | // WARNING: the string in the input will be duplicated in the JsonDocument. |
| 20 | String input = |
| 21 | "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}"; |
| 22 | deserializeJson(doc, input); |
| 23 | |
| 24 | // You can use a String as a key to get a member from JsonDocument |
| 25 | // No duplication is done. |
| 26 | long time = doc[String("time")]; |
| 27 | |
| 28 | // You can use a String as a key to set a member of a JsonDocument |
| 29 | // WARNING: the content of the String will be duplicated in the JsonDocument. |
| 30 | doc[String("time")] = time; |
| 31 | |
| 32 | // You can get the content of a JsonVariant as a String |
| 33 | // No duplication is done, at least not in the JsonDocument. |
| 34 | String sensor = doc["sensor"]; |
| 35 | |
| 36 | // Unfortunately, the following doesn't work (issue #118): |
| 37 | // sensor = doc["sensor"]; // <- error "ambiguous overload for 'operator='" |
| 38 | // As a workaround, you need to replace by: |
| 39 | sensor = doc["sensor"].as<String>(); |
| 40 | |
| 41 | // You can set a String as the content of a JsonVariant |
| 42 | // WARNING: the content of the String will be duplicated in the JsonDocument. |
| 43 | doc["sensor"] = sensor; |
| 44 | |
| 45 | // It works with serialized() too: |
| 46 | doc["sensor"] = serialized(sensor); |
| 47 | |
| 48 | // You can also concatenate strings |
| 49 | // WARNING: the content of the String will be duplicated in the JsonDocument. |
| 50 | doc[String("sen") + "sor"] = String("gp") + "s"; |
| 51 | |
| 52 | // You can compare the content of a JsonObject with a String |
| 53 | if (doc["sensor"] == sensor) { |
| 54 | // ... |
| 55 | } |
| 56 | |
| 57 | // Lastly, you can print the resulting JSON to a String |
| 58 | String output; |
| 59 | serializeJson(doc, output); |
| 60 | } |
| 61 | |
| 62 | void loop() { |
| 63 | // not used in this example |
| 64 | } |
| 65 | |
| 66 | // See also |
| 67 | // -------- |
| 68 | // |
| 69 | // https://arduinojson.org/ contains the documentation for all the functions |
| 70 | // used above. It also includes an FAQ that will help you solve any problem. |
| 71 | // |
| 72 | // The book "Mastering ArduinoJson" contains a quick C++ course that explains |
| 73 | // how your microcontroller stores strings in memory. On several occasions, it |
| 74 | // shows how you can avoid String in your program. |
| 75 | // Learn more at https://arduinojson.org/book/ |
| 76 | // Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤ |