c++ nlohmann json - how to iterate / find a nested object
Indeed, iteration does not recurse and there is no library function for this (yet). What about:
#include "json.hpp"#include <iostream>using json = nlohmann::json;template<class UnaryFunction>void recursive_iterate(const json& j, UnaryFunction f){ for(auto it = j.begin(); it != j.end(); ++it) { if (it->is_structured()) { recursive_iterate(*it, f); } else { f(it); } }}int main(){ json j = {{"one", 1}, {"two", 2}, {"three", {"three.one", 3.1}}}; recursive_iterate(j, [](json::const_iterator it){ std::cout << *it << std::endl; });}
The output is:
1"three.one"3.12