Description

The member functions begin() and end() return STL-style iterators. You can use these iterators to enumerate all the element 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>();

// using C++11 syntax (preferred):
for (JsonVariant value : arr) {
    Serial.println(value.as<char*>());
}

// using C++98 syntax (for older compilers):
for (JsonArray::iterator it=arr.begin(); it!=arr.end(); ++it) {
    Serial.println(it->as<char*>());
}

The code above would print:

one
two
three

See also