JsonObject::createNestedArray()
Description
JsonObject::createNestedArray()
adds a new array to the object pointed by the JsonObject
.
If the JsonObject
is null, this function does nothing.
Signatures
JsonArray createNestedArray(char* key) const;
JsonArray createNestedArray(const char* key) const;
JsonArray createNestedArray(const __FlashStringHelper* key) const;
JsonArray createNestedArray(const String& key) const;
JsonArray createNestedArray(const std::string& key) const;
JsonArray createNestedArray(std::string_view key) const; // 🆕 (added in 6.18.1)
Arguments
key
: the key to associate with the array.
As usual, ArduinoJson makes a copy of a string in the JsonDocument
, except if it’s a const char*
.
String duplication
ArduinoJson makes a copy of the string when you call this function with one of the following types:
char*
String
(orstd::string
)const __FlashStringHelper*
(i.e. Flash string)
This duplication consumes some space in the JsonDocument
; don’t forget to increase its capacity accordingly.
Return value
JsonObject::createNestedArray()
returns a JsonArray
that points to the newly created array.
JsonObject::createNestedArray()
return null if the memory allocation fails; in which case JsonArray::isNull()
, returns true
.
Example
StaticJsonDocument<256> doc;
JsonObject root = doc.to<JsonObject>();
root["status"] = "on";
JsonArray levels = root.createNestedArray("levels");
levels.add(10);
levels.add(20);
serializeJsonPretty(root, Serial);
will print
{
"status": "on",
"levels": [
10,
20
]
}