operatorjson_pointer_const.cpp 648 B

123456789101112131415161718192021222324
  1. #include <iostream>
  2. #include <nlohmann/json.hpp>
  3. using json = nlohmann::json;
  4. int main()
  5. {
  6. // create a JSON value
  7. const json j =
  8. {
  9. {"number", 1}, {"string", "foo"}, {"array", {1, 2}}
  10. };
  11. // read-only access
  12. // output element with JSON pointer "/number"
  13. std::cout << j["/number"_json_pointer] << '\n';
  14. // output element with JSON pointer "/string"
  15. std::cout << j["/string"_json_pointer] << '\n';
  16. // output element with JSON pointer "/array"
  17. std::cout << j["/array"_json_pointer] << '\n';
  18. // output element with JSON pointer "/array/1"
  19. std::cout << j["/array/1"_json_pointer] << '\n';
  20. }