You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
786 B

  1. #include <json.hpp>
  2. using json = nlohmann::json;
  3. int main()
  4. {
  5. // create a JSON object with different entry types
  6. json j =
  7. {
  8. {"integer", 1},
  9. {"floating", 42.23},
  10. {"string", "hello world"},
  11. {"boolean", true},
  12. {"object", {{"key1", 1}, {"key2", 2}}},
  13. {"array", {1, 2, 3}}
  14. };
  15. // access existing values
  16. int v_integer = j.value("integer", 0);
  17. double v_floating = j.value("floating", 47.11);
  18. // access nonexisting values and rely on default value
  19. std::string v_string = j.value("nonexisting", "oops");
  20. bool v_boolean = j.value("nonexisting", false);
  21. // output values
  22. std::cout << std::boolalpha << v_integer << " " << v_floating
  23. << " " << v_string << " " << v_boolean << "\n";
  24. }