JsonObject::operator[]
Description
JsonObject::operator[]
gets, replaces, or adds a value in the object pointed by the JsonObject
.
If the JsonObject
is null/unbound, this function does nothing.
All values are stored by copy, except string literals which are stored by address.
The string copy policy has changed in ArduinoJson 7.3.
If you need to know if the insertion succeed, use JsonVariant::set()
instead.
Signatures
MemberProxy operator[](const char (&value)[N]) const;
MemberProxy operator[](const char* key) const;
MemberProxy operator[](const __FlashStringHelper* key) const;
MemberProxy operator[](const String& key) const;
MemberProxy operator[](const std::string& key) const;
MemberProxy operator[](std::string_view key) const;
Arguments
key
: the key that the value will be associated with.
Return value
JsonObject::operator[]
return a proxy class that allows using the JsonObject
like a dictionary.
If this concept of proxy class is unfamiliar to you, just think of a JsonVariant
instead.
The proxy class is non-copyable, so you cannot store it in a variable; you must use it immediately. This implies that you cannot use the auto
keyword to store the result of this operator; instead, you must specify the type explicitly as show in the example below.
Example
JsonDocument doc;
JsonObject object = doc.to<JsonObject>();
object["hello"] = "world";
const char* world = object["hello"];
If you want to use the auto
keyword, you must specify the type explicitly:
auto world = object["hello"].as<const char*>();