JsonVariant::as<T>() casts the value pointed by the JsonVariant to the specified type.

Unlike JsonVariant::to<T>(), this function doesn’t modify the value pointed by the JsonVariant.


bool               as<bool>() const;

float              as<float>() const;
double             as<double>() const;

signed char        as<signed char>() const;
unsigned char      as<unsigned char>() const;
signed int         as<signed int>() const;
unsigned int       as<unsigned int>() const;
signed short       as<signed short>() const;
unsigned short     as<unsigned short>() const;
signed long        as<signed long>() const;
unsigned long      as<unsigned long>() const;
unsigned long long as<unsigned long long>() const;   // <- may require ARDUINOJSON_USE_LONG_LONG
signed long long   as<signed long long>() const;     // <- may require ARDUINOJSON_USE_LONG_LONG
signed __int64     as<signed __int64>() const;       // <- may require ARDUINOJSON_USE_INT64
unsigned __int64   as<unsigned __int64>() const;     // <- may require ARDUINOJSON_USE_INT64

const char*        as<char*>() const;
const char*        as<const char*>() const;
String             as<String>() const;
std::string        as<std::string>() const;

JsonArray          as<JsonArray>() const;
JsonObject         as<JsonObject>() const;

Return value

JsonVariant::as<T>() returns the value pointed by the JsonVariant casted to the specified type.

This function returns a default value if the cast is not possible. The default value is:

If you want to change the default value, you need to use JsonVariant::operator| instead.


StaticJsonDocument<256> doc;
deserializeJson(doc, "{\"answer\":42}");

JsonVariant answer = doc["answer"];
int i =<int>(); // <- i == 42
double d =<double>(); // <- d == 42.0
const char* s =<char*>(); // <- s == NULL

See also