JsonObject::createNestedObject()
Description
JsonObject::createNestedObject()
appends a new object to the object pointed by the JsonObject
.
If the JsonObject
is null, this function does nothing.
Signature
JsonObject createNestedObject(char* key) const;
JsonObject createNestedObject(const char* key) const;
JsonObject createNestedObject(const __FlashStringHelper* key) const;
JsonObject createNestedObject(const String& key) const; // see Remarks
JsonObject createNestedObject(const std::string& key) const; // see Remarks
JsonObject createNestedObject(std::string_view key) const; // 🆕 (added in 6.18.1)
Arguments
key
: the key to associate with the new object.
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::createNestedObject()
returns a JsonObject
that points to the newly created object.
JsonObject::createNestedObject()
returns null if the memory allocation fails; in which case JsonObject::isNull()
, returns true
.
Example
StaticJsonDocument<256> doc;
JsonObject root = doc.to<JsonObject>();
root["city"] = "Paris";
JsonObject weather = root.createNestedObject("weather");
weather["temp"] = 14.2;
weather["cond"] = "cloudy";
serializeJsonPretty(root, Serial);
will print
{
"city": "Paris",
"weather": {
"temp": 14.20,
"cond": "cloudy"
}
}