update.cpp 691 B

123456789101112131415161718192021222324
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <nlohmann/json.hpp>
  4. using json = nlohmann::json;
  5. using namespace nlohmann::literals;
  6. int main()
  7. {
  8. // create two JSON objects
  9. json o1 = R"( {"color": "red", "price": 17.99, "names": {"de": "Flugzeug"}} )"_json;
  10. json o2 = R"( {"color": "blue", "speed": 100, "names": {"en": "plane"}} )"_json;
  11. json o3 = o1;
  12. // add all keys from o2 to o1 (updating "color", replacing "names")
  13. o1.update(o2);
  14. // add all keys from o2 to o1 (updating "color", merging "names")
  15. o3.update(o2, true);
  16. // output updated object o1 and o3
  17. std::cout << std::setw(2) << o1 << '\n';
  18. std::cout << std::setw(2) << o3 << '\n';
  19. }