JSON for Modern C++ 3.8.0

accept
accept
accept
allocator_type
array
array_t
at
at
at
at
at
at
back
back
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
begin
begin
binary
binary
binary
binary
binary_t
boolean_t
cbegin
cbor_tag_handler_t
cend
clear
const_iterator
const_pointer
const_reference
const_reverse_iterator
contains
contains
count
crbegin
crend
detail::external_constructor
diff
difference_type
dump
emplace
emplace_back
empty
end
end
erase
erase
erase
erase
error_handler_t
exception
find
find
flatten
from_bson
from_bson
from_bson
from_bson
from_cbor
from_cbor
from_cbor
from_cbor
from_msgpack
from_msgpack
from_msgpack
from_msgpack
from_ubjson
from_ubjson
from_ubjson
from_ubjson
front
front
get
get
get
get
get
get
get_allocator
get_binary
get_binary
get_ptr
get_ptr
get_ref
get_ref
get_to
get_to
get_to
initializer_list_t
input_format_t
insert
insert
insert
insert
insert
insert
insert_iterator
invalid_iterator
is_array
is_binary
is_boolean
is_discarded
is_null
is_number
is_number_float
is_number_integer
is_number_unsigned
is_object
is_primitive
is_string
is_structured
items
items
iterator
iterator_wrapper
iterator_wrapper
json_pointer
json_sax_t
json_serializer
max_size
merge_patch
meta
number_float_t
number_integer_t
number_unsigned_t
object
object_comparator_t
object_t
operator value_t
operator ValueType
operator!=
operator!=
operator!=
operator+=
operator+=
operator+=
operator+=
operator<
operator<
operator<
operator<<
operator<<
operator<=
operator<=
operator<=
operator=
operator==
operator==
operator==
operator>
operator>
operator>
operator>=
operator>=
operator>=
operator>>
operator>>
operator[]
operator[]
operator[]
operator[]
operator[]
operator[]
operator[]
operator[]
other_error
out_of_range
parse
parse
parse
parse_error
parse_event_t
parser_callback_t
patch
pointer
push_back
push_back
push_back
push_back
rbegin
rbegin
reference
rend
rend
reverse_iterator
sax_parse
sax_parse
sax_parse
size
size_type
string_t
swap
swap
swap
swap
swap
swap
swap
to_bson
to_bson
to_bson
to_cbor
to_cbor
to_cbor
to_msgpack
to_msgpack
to_msgpack
to_ubjson
to_ubjson
to_ubjson
type
type_error
type_name
unflatten
update
update
value
value
value
value
value_t
value_type
~basic_json
| ## ◆ value() [3/4]
string_t nlohmann::basic_json::value | ( | const typename object_t::key_type & | key,
|  | const char * | default_value
| ) |  | const
| inline
  access specified object element with default value Returns either a copy of an object's element at the specified key key or a given default value if no element with key key exists. The function is basically equivalent to executing try { return at(key); } catch(out_of_range) { return default_value; } Note Unlike at(const typename object_t::key_type&), this function does not throw if the given key key was not found. Unlike operator[](const typename object_t::key_type& key), this function does not implicitly add an element to the position defined by key. This function is furthermore also applicable to const objects. Parameters
  [in] | key | key of the element to access
  [in] | default_value | the value to return if key is not found
  Template Parameters
  ValueType | type compatible to JSON values, for instance `int` for JSON integer numbers, `bool` for JSON booleans, or `std::vector` types for JSON arrays. Note the type of the expected value at key and the default value default_value must be compatible.
  Returns copy of the element at key key or default_value if key is not found Exceptions
  type_error.302 | if default_value does not match the type of the value at key
  type_error.306 | if the JSON value is not an object; in that case, using `value()` with a key makes no sense.
  Complexity Logarithmic in the size of the container. Example The example below shows how object elements can be queried with a default value. 1 #include <iostream> 2 #include <nlohmann/json.hpp> 3 4 using json = nlohmann::json; 5 6 int main() 7 { 8 // create a JSON object with different entry types 9 json j = 10 { 11 {"integer", 1}, 12 {"floating", 42.23}, 13 {"string", "hello world"}, 14 {"boolean", true}, 15 {"object", {{"key1", 1}, {"key2", 2}}}, 16 {"array", {1, 2, 3}} 17 }; 18 19 // access existing values 20 int v_integer = j.value("integer", 0); 21 double v_floating = j.value("floating", 47.11); 22 23 // access nonexisting values and rely on default value 24 std::string v_string = j.value("nonexisting", "oops"); 25 bool v_boolean = j.value("nonexisting", false); 26 27 // output values 28 std::cout << std::boolalpha << v_integer << " " << v_floating 29 << " " << v_string << " " << v_boolean << "\n"; 30 } Output (play with this example online):
      1 42.23 oops false
  The example code above can be translated with
      g++ -std=c++11 -Isingle_include doc/examples/basic_json__value.cpp -o basic_json__value
See also at(const typename object_t::key_type&) for access by reference with range checking operator[](const typename object_t::key_type&) for unchecked access by reference Since version 1.0.0 Definition at line 20211 of file json.hpp.

nlohmann::basic_json::at
reference at(size_type idx)
access specified array element with bounds checking
Definition: json.hpp:19693
nlohmann::basic_json::out_of_range
detail::out_of_range out_of_range
exception indicating access out of the defined range
Definition: json.hpp:16647
nlohmann::json
basic_json<> json
default JSON class
Definition: json.hpp:2933