README.cpp 753 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <nlohmann/json.hpp>
  4. using json = nlohmann::json;
  5. int main()
  6. {
  7. // create a JSON object
  8. json j =
  9. {
  10. {"pi", 3.141},
  11. {"happy", true},
  12. {"name", "Niels"},
  13. {"nothing", nullptr},
  14. {
  15. "answer", {
  16. {"everything", 42}
  17. }
  18. },
  19. {"list", {1, 0, 2}},
  20. {
  21. "object", {
  22. {"currency", "USD"},
  23. {"value", 42.99}
  24. }
  25. }
  26. };
  27. // add new values
  28. j["new"]["key"]["value"] = {"another", "list"};
  29. // count elements
  30. auto s = j.size();
  31. j["size"] = s;
  32. // pretty print with indent of 4 spaces
  33. std::cout << std::setw(4) << j << '\n';
  34. }