JsonArray::begin() / JsonArray::end()
Description
The member functions begin()
and end()
return STL-style iterators. You can use these iterators to enumerate all the elements in the array pointed by the JsonArray
.
These functions reproduce the containers in the C++ Standard Library and allow you to use the “ranged-based for loop” feature of C++11. See the example below.
Signatures
JsonArray::iterator begin() const;
JsonArray::iterator end() const;
Return value
begin()
returns an iterator to the first element of the array.
end()
returns an iterator to the element after the last. This iterator must not be dereferenced because it is out of the array. It’s a placeholder to detect the end of the array.
JsonArray::iterator
points to a JsonVariant
. You can dereference the iterator with the usual *
and ->
operators.
Example
char json[] = "[\"one\",\"two\",\"three\"]";
DynamicJsonDocument doc(1024);
deserializeJson(doc, json);
JsonArray arr = doc.as<JsonArray>();
for (JsonVariant value : arr) {
Serial.println(value.as<const char*>());
}
The code above would print:
one
two
three
See also
JsonObject::begin()
/JsonObject::end()
- To recursively walk the
JsonDocument
, see the “Recursive Analyzer” case study in Mastering ArduinoJson