ARDUINOJSON_ENABLE_ARDUINO_STRING
Enables support of the type String
in the library.
The default is 1
of ARDUINO
is defined, 0
otherwise.
In other words, String
is supported as soon as you work in an Arduino-compatible environment
How to force the value?
If you need to force the support of String
, add this at the top of your program:
#define ARDUINOJSON_ENABLE_ARDUINO_STRING 1
#include <ArduinoJson.h>
On the other hand, if you need to disable String
, do:
#define ARDUINOJSON_ENABLE_ARDUINO_STRING 0
#include <ArduinoJson.h>
Where a String
can be used?
Once enabled, you can use a String
in many places.
-
You can use a String as your JSON input
// WARNING: the content of the String will be duplicated in the JsonBuffer. String input = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}"; JsonObject& root = jsonBuffer.parseObject(input);
-
You can use a String to get an element of a JsonObject
long time = root[String("time")];
-
You can use a String to set an element of a JsonObject
// WARNING: the content of the String will be duplicated in the JsonBuffer. root[String("time")] = time;
-
You can get a String from a JsonObject or JsonArray
String sensor = root["sensor"];
-
You can set a String to a JsonObject or JsonArray:
// WARNING: the content of the String will be duplicated in the JsonBuffer. root["sensor"] = sensor;
-
You can also concatenate strings
// WARNING: the content of the String will be duplicated in the JsonBuffer. root[String("sen") + "sor"] = String("gp") + "s";
-
You can compare the content of a JsonObject with a String
if (root["sensor"] == sensor) { // ... }
-
You can print the resulting JSON to a String
String output; root.printTo(output);
Where a String
can not be used?
-
Unfortunately, the following doesn’t work (issue #118):
String sensor = "something"; sensor = root["sensor"]; // <- error "ambiguous overload for 'operator='"
-
As a workaround, you need to replace by:
sensor = root["sensor"].as<String>();