template<template< typename U, typename V, typename... Args > class ObjectType = std::map, template< typename U, typename... Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer, class BinaryType = std::vector<std::uint8_t>>
template<typename T >
const_reference nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType >::operator[] |
( |
T * |
key | ) |
const |
|
inline |
read-only access specified object element
Returns a const reference to the element at with specified key key. No bounds checking is performed.
- Warning
- If the element with key key does not exist, the behavior is undefined.
- Parameters
-
[in] | key | key of the element to access |
- Returns
- const reference to the element at key key
- Precondition
- The element with key key must exist. This precondition is enforced with an assertion.
- Exceptions
-
type_error.305 | if the JSON value is not an object; in that case, using the [] operator with a key makes no sense. |
@complexity Logarithmic in the size of the container.
@liveexample{The example below shows how object elements can be read using the []
operator.,operatorarray__key_type_const}
- See also
- see at(const typename object_t::key_type&) for access by reference with range checking
-
see value() for access by value with a default value
- Since
- version 1.1.0
Definition at line 21616 of file json.hpp.
21617 {
21618
21620 {
21621 JSON_ASSERT(
m_value.object->find(key) !=
m_value.object->end());
21622 return m_value.object->find(key)->second;
21623 }
21624
21625 JSON_THROW(type_error::create(305,
"cannot use operator[] with a string argument with " + std::string(
type_name()), *
this));
21626 }
constexpr bool is_object() const noexcept
return whether value is an object
json_value m_value
the value of the current element
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string