Description

Allows the JsonVariant to mimic a JsonArray or a JsonObject.

You can use this operator to chain the access to a nested member:

const char* weather = obj["weather"][0]["description"];

Signatures

// mimics a JsonArray
JsonVariant& operator[](size_t index);

// mimics a const JsonArray
const JsonVariant& operator[](size_t index) const;

// mimics a JsonObject
JsonVariant& operator[](const char* key);
JsonVariant& operator[](char* key); // see Remarks
JsonVariant& operator[](const String& key); // see Remarks
JsonVariant& operator[](const std::string& key); // see Remarks
JsonVariant& operator[](const __FlashStringHelper* key); // see Remarks

// mimics a const JsonObject
const JsonVariant& operator[](const char* key) const;
const JsonVariant& operator[](const String& key) const;
const JsonVariant& operator[](const std::string& key) const;
const JsonVariant& operator[](const __FlashStringHelper* key) const;

Arguments

index: the index in the JsonArray.

key: the key in the JsonObject

Return value

The value at specified index or key, or an undefined JsonVariant if the value doesn’t exist.

Remarks

ArduinoJson makes a copy of the string when you call this function with one of the following types:

  • char*
  • String (or std::string)
  • const __FlashStringHelper * (i.e. Flash string)

This duplication causes the JsonBuffer to grow. The memory allocated for the copy will only be freed when the whole JsonBuffer is discarded.

Note that the rules changed in version 5.13.

Example

JsonVariant variant;

variant = doc.parse("{\"hello\":\"world\"}");
const char* hello = variant["hello"];

variant = doc.parse("[\"hello\",\"world\"]");
const char* world = variant[1];

See also