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
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
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
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
| ## ◆ at() [1/6]
reference nlohmann::basic_json::at | ( | const json_pointer & | ptr | ) |
| inline
  Returns a reference to the element at with specified JSON pointer ptr, with bounds checking. Parameters
  [in] | ptr | JSON pointer to the desired element
  Returns reference to the element pointed to by ptr Exceptions
  parse_error.106 | if an array index in the passed JSON pointer ptr begins with '0'. See example below.
  parse_error.109 | if an array index in the passed JSON pointer ptr is not a number. See example below.
  out_of_range.401 | if an array index in the passed JSON pointer ptr is out of range. See example below.
  out_of_range.402 | if the array index '-' is used in the passed JSON pointer ptr. As `at` provides checked access (and no elements are implicitly inserted), the index '-' is always invalid. See example below.
  out_of_range.403 | if the JSON pointer describes a key of an object which cannot be found. See example below.
  out_of_range.404 | if the JSON pointer ptr can not be resolved. See example below.
  Exception safety Strong guarantee: if an exception is thrown, there are no changes in the JSON value. Complexity Constant. Since version 2.0.0 Example The behavior is shown in the example. 1 #include <iostream> 2 #include <nlohmann/json.hpp> 3 4 using json = nlohmann::json; 5 6 int main() 7 { 8 // create a JSON value 9 json j = 10 { 11 {"number", 1}, {"string", "foo"}, {"array", {1, 2}} 12 }; 13 14 // read-only access 15 16 // output element with JSON pointer "/number" 17 std::cout << j.at("/number"_json_pointer) << '\n'; 18 // output element with JSON pointer "/string" 19 std::cout << j.at("/string"_json_pointer) << '\n'; 20 // output element with JSON pointer "/array" 21 std::cout << j.at("/array"_json_pointer) << '\n'; 22 // output element with JSON pointer "/array/1" 23 std::cout << j.at("/array/1"_json_pointer) << '\n'; 24 25 // writing access 26 27 // change the string 28 j.at("/string"_json_pointer) = "bar"; 29 // output the changed string 30 std::cout << j["string"] << '\n'; 31 32 // change an array element 33 j.at("/array/1"_json_pointer) = 21; 34 // output the changed array 35 std::cout << j["array"] << '\n'; 36 37 38 // out_of_range.106 39 try 40 { 41 // try to use an array index with leading '0' 42 json::reference ref = j.at("/array/01"_json_pointer); 43 } 44 catch (json::parse_error& e) 45 { 46 std::cout << e.what() << '\n'; 47 } 48 49 // out_of_range.109 50 try 51 { 52 // try to use an array index that is not a number 53 json::reference ref = j.at("/array/one"_json_pointer); 54 } 55 catch (json::parse_error& e) 56 { 57 std::cout << e.what() << '\n'; 58 } 59 60 // out_of_range.401 61 try 62 { 63 // try to use a an invalid array index 64 json::reference ref = j.at("/array/4"_json_pointer); 65 } 66 catch (json::out_of_range& e) 67 { 68 std::cout << e.what() << '\n'; 69 } 70 71 // out_of_range.402 72 try 73 { 74 // try to use the array index '-' 75 json::reference ref = j.at("/array/-"_json_pointer); 76 } 77 catch (json::out_of_range& e) 78 { 79 std::cout << e.what() << '\n'; 80 } 81 82 // out_of_range.403 83 try 84 { 85 // try to use a JSON pointer to an nonexistent object key 86 json::const_reference ref = j.at("/foo"_json_pointer); 87 } 88 catch (json::out_of_range& e) 89 { 90 std::cout << e.what() << '\n'; 91 } 92 93 // out_of_range.404 94 try 95 { 96 // try to use a JSON pointer that cannot be resolved 97 json::reference ref = j.at("/number/foo"_json_pointer); 98 } 99 catch (json::out_of_range& e) 100 { 101 std::cout << e.what() << '\n'; 102 } 103 } Output (play with this example online):
      1
      "foo"
      [1,2]
      2
      "bar"
      [1,21]
      [json.exception.parse_error.106] parse error: array index '01' must not begin with '0'
      [json.exception.parse_error.109] parse error: array index 'one' is not a number
      [json.exception.out_of_range.401] array index 4 is out of range
      [json.exception.out_of_range.402] array index '-' (2) is out of range
      [json.exception.out_of_range.403] key 'foo' not found
      [json.exception.out_of_range.404] unresolved reference token 'foo'
  The example code above can be translated with
      g++ -std=c++11 -Isingle_include doc/examples/at_json_pointer.cpp -o at_json_pointer
Definition at line 23727 of file json.hpp.

nlohmann::basic_json::at
reference at(size_type idx)
access specified array element with bounds checking
Definition: json.hpp:19069
nlohmann::basic_json::const_reference
const value_type & const_reference
the type of an element const reference
Definition: json.hpp:16058
nlohmann::basic_json::out_of_range
detail::out_of_range out_of_range
exception indicating access out of the defined range
Definition: json.hpp:16036
nlohmann::basic_json::parse_error
detail::parse_error parse_error
exception indicating a parse error
Definition: json.hpp:16030
nlohmann::basic_json::reference
value_type & reference
the type of an element reference
Definition: json.hpp:16056
nlohmann::json
basic_json<> json
default JSON class
Definition: json.hpp:2774