push_back__initializer_list.cpp 611 B

123456789101112131415161718192021222324252627
  1. #include <iostream>
  2. #include <nlohmann/json.hpp>
  3. using json = nlohmann::json;
  4. int main()
  5. {
  6. // create JSON values
  7. json object = {{"one", 1}, {"two", 2}};
  8. json null;
  9. // print values
  10. std::cout << object << '\n';
  11. std::cout << null << '\n';
  12. // add values:
  13. object.push_back({"three", 3}); // object is extended
  14. object += {"four", 4}; // object is extended
  15. null.push_back({"five", 5}); // null is converted to array
  16. // print values
  17. std::cout << object << '\n';
  18. std::cout << null << '\n';
  19. // would throw:
  20. //object.push_back({1, 2, 3});
  21. }