JSON for Modern C++ 2.0.3

- Main Page
- Related Pages
- Classes

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
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
begin
begin
boolean_t
cbegin
cend
clear
const_pointer
const_reference
const_reverse_iterator
count
crbegin
crend
diff
difference_type
dump
empty
end
end
erase
erase
erase
erase
find
find
flatten
front
front
get
get
get
get_allocator
get_ptr
get_ptr
get_ref
get_ref
insert
insert
insert
insert
insert
is_array
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
iterator_wrapper
iterator_wrapper
max_size
number_float_t
number_integer_t
number_unsigned_t
object
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[]
parse
parse
parse
parse
parse
parse
parse_event_t
parser_callback_t
patch
pointer
push_back
push_back
push_back
push_back
rbegin
rbegin
reference
rend
rend
reverse_iterator
size
size_type
string_t
swap
swap
swap
swap
type
unflatten
value
value
value
value
value_t
value_type
~basic_json
| 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 ValueType , typename std::enable_if< not std::is_pointer< ValueType >::value, int >::type = 0>
ValueType nlohmann::basic_json::get | ( |  | ) | const
| inline
  Explicit type conversion between the JSON value and a compatible value. Template Parameters
  ValueType | non-pointer type compatible to the JSON value, for instance `int` for JSON integer numbers, `bool` for JSON booleans, or `std::vector` types for JSON arrays
  Returns copy of the JSON value, converted to type ValueType Exceptions
  std::domain_error | in case passed type ValueType is incompatible to JSON; example: `"type must be object, but is null"`
  Complexity Linear in the size of the JSON value. Example The example below shows several conversions from JSON values to other types. There a few things to note: (1) Floating-point numbers can be converted to integers, (2) A JSON array can be converted to a standard `std::vector<short>`, (3) A JSON object can be converted to C++ associative containers such as `std::unordered_map<std::string, json>`. 1 #include <json.hpp> 2 #include <unordered_map> 3 4 using json = nlohmann::json; 5 6 int main() 7 { 8 // create a JSON value with different types 9 json json_types = 10 { 11 {"boolean", true}, 12 { 13 "number", { 14 {"integer", 42}, 15 {"floating-point", 17.23} 16 } 17 }, 18 {"string", "Hello, world!"}, 19 {"array", {1, 2, 3, 4, 5}}, 20 {"null", nullptr} 21 }; 22 23 // use explicit conversions 24 auto v1 = json_types["boolean"].get<bool>(); 25 auto v2 = json_types["number"]["integer"].get<int>(); 26 auto v3 = json_types["number"]["integer"].get<short>(); 27 auto v4 = json_types["number"]["floating-point"].get<float>(); 28 auto v5 = json_types["number"]["floating-point"].get<int>(); 29 auto v6 = json_types["string"].get<std::string>(); 30 auto v7 = json_types["array"].get<std::vector<short>>(); 31 auto v8 = json_types.get<std::unordered_map<std::string, json>>(); 32 33 // print the conversion results 34 std::cout << v1 << '\n'; 35 std::cout << v2 << ' ' << v3 << '\n'; 36 std::cout << v4 << ' ' << v5 << '\n'; 37 std::cout << v6 << '\n'; 38 39 for (auto i : v7) 40 { 41 std::cout << i << ' '; 42 } 43 std::cout << "\n\n"; 44 45 for (auto i : v8) 46 { 47 std::cout << i.first << ": " << i.second << '\n'; 48 } 49 } nlohmann::json basic_json<> json default JSON class Definition: json.hpp:10512 Output (play with this example online):
      1
      42 42
      17.23 17
      Hello, world!
      1 2 3 4 5

      string: "Hello, world!"
      number: {"floating-point":17.23,"integer":42}
      null: null
      boolean: true
      array: [1,2,3,4,5]
  The example code above can be translated with
      g++ -std=c++11 -Isrc doc/examples/get__ValueType_const.cpp -o get__ValueType_const
See also operator ValueType() const for implicit conversion get() for pointer-member access Since version 1.0.0 Definition at line 2897 of file json.hpp.