My problem is using json_pointers correctly, let me explain better by starting to show you a JSon structure:
{
"bar": {
"Qt": "6.8",
"json": "3.11.3",
"array_json": ["/bar/Qt", "/bar/json"]
},
"foo": null
}
< /code>
Хорошо, теперь мой пример кода-это использование C ++ 23: < /p>
#include
#include
using json = nlohmann::ordered_json;
int main()
try {
json data = R"(
{
"bar": {
"Qt": "6.8",
"json": "3.11.3",
"array_json": ["/bar/Qt", "/bar/json"]
},
"foo": null
}
)"_json;
std::println("nlohmann/json version: {}.{}.{}",
NLOHMANN_JSON_VERSION_MAJOR, NLOHMANN_JSON_VERSION_MINOR, NLOHMANN_JSON_VERSION_PATCH);
// get the second element of the "array_json"
auto s_pointer = data["bar"]["array_json"][1].get();
// expected value of pointer = "/bar/json"
std::println("{}", s_pointer);
// create a json_pointer
json::json_pointer j_pointer(data["bar"]["array_json"][1].get());
// expected value of json pointer = "/bar/json"
std::println("{}", j_pointer.to_string());
// get the value of /bar/json using json pointer
auto value = data[j_pointer].get(); // warning here, but its working fine...
std::println("{}", value); // "3.11.3"
// my try to surpress the warning
value = data[j_pointer.to_string()].get(); // no warning but dont work properly
// error : terminate called after throwing an instance of 'nlohmann::json_abi_v3_11_3::detail::type_error'
// what(): [json.exception.type_error.302] type must be string, but is null
std::println("{}", value);
return 0;
}
catch (const std::exception& e) {
std::print("Exception: {}\n", e.what());
return 1;
}
< /code>
Здесь сведения о предупреждении я получил ... < /p>
main.cpp:92:53: 'operator ==' Умерен: с 3.11.2; Используйте Operator == (json_pointer, json_pointer) < /p>
< /blockquote>
p>json.hpp
json.hpp
Подробнее здесь: https://stackoverflow.com/questions/795 ... ated-since
Мобильная версия