JsonArrayConst::begin() / JsonArrayConst::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 JsonArrayConst
.
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
JsonArrayConst::iterator begin() const;
JsonArrayConst::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.
JsonArrayConst::iterator
points to a JsonVariantConst
. You can dereference the iterator with the usual *
and ->
operators.
Example
char json[] = "[\"one\",\"two\",\"three\"]";
DynamicJsonDocument doc(1024);
deserializeJson(doc, json);
JsonArrayConst arr = doc.as<JsonArray>();
for (JsonVariantConst value : arr) {
Serial.println(value.as<const char*>());
}
The code above would print:
one
two
three
See also
JsonArray::begin()
/JsonArray::end()
- To recursively walk the
JsonDocument
, see the “Recursive Analyzer” case study in Mastering ArduinoJson