JsonArray::operator[]
Description
JsonArray::operator[]
gets or replaces a value in the array pointed by the JsonArray
.
If the JsonArray
is null, this operator does nothing.
If you need to know if the insertion succeed, use JsonVariant::set()
instead.
Don’t use this operator to get all values; instead, use iterators.
Signatures
ElementProxy operator[](size_t index) const;
Argument
index
: the zero-based position of the value in the array.
Return value
JsonArray::operator[]
returns a proxy class that allows you to use the JsonArray
as a C array.
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
JsonArray array = doc.to<JsonArray>();
array.add(42);
int value = array[0];
array[0] = 666;
If you want to use the auto
keyword, you must specify the type explicitly:
auto value = array[0].as<int>();