count.cpp 465 B

123456789101112131415161718
  1. #include <iostream>
  2. #include <nlohmann/json.hpp>
  3. using json = nlohmann::json;
  4. int main()
  5. {
  6. // create a JSON object
  7. json j_object = {{"one", 1}, {"two", 2}};
  8. // call count()
  9. auto count_two = j_object.count("two");
  10. auto count_three = j_object.count("three");
  11. // print values
  12. std::cout << "number of elements with key \"two\": " << count_two << '\n';
  13. std::cout << "number of elements with key \"three\": " << count_three << '\n';
  14. }