Description

Clears the JsonDocument and converts it to the specified type.

Signature

JsonArray   to<JsonArray>();
JsonObject  to<JsonObject>();
JsonVariant to<JsonVariant>();

Example

// allocate the JsonDocument
StaticJsonDocument<200> doc;

// convert it to a JsonObject
JsonObject root = doc.to<JsonObject>();

// set values
root["hello"] = "world";

Invalidates references

Because this function releases memory owned by the JsonDocument, it invalidates all references acquired before.

Example:

DynamicJsonDocument doc(1024);

JsonObject root1 = doc.to<JsonObject>();

JsonObject root2 = doc.to<JsonObject>();

// Don't use root1 here, because it's dangling!

See also

Global warming stripes by Professor Ed Hawkins (University of Reading)