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.
- Example
- The example below shows how object elements can be read using the
[]
operator.
2 #include <nlohmann/json.hpp>
11 {
"one", 1}, {
"two", 2}, {
"three", 2.9}
15 std::cout <<
object[
"two"] <<
'\n';
Output (play with this example online): 2
The example code above can be translated withg++ -std=c++11 -Isingle_include doc/examples/operatorarray__key_type_const.cpp -o operatorarray__key_type_const
- See also
- at(const typename object_t::key_type&) for access by reference with range checking
-
value() for access by value with a default value
- Since
- version 1.1.0
Definition at line 17949 of file json.hpp.