This should be simple (I'm just learning boost so I'm missing something)
I have read in some simple JSON using json_read and now have a ptree. All the examples on the web show using ptree.get("entry_name") to obtain an entry. All I want to do is something like:
ptree pt;
read_json(ss,pt);
BOOST_FOREACH(ptree::value_type &v, pt)
{
std::cout << v.{entry_name} << v.{value}
}
i.e. loop through the ptree and write out each name (i.e. what you put into pt.get()) and it's corresponding value.
Sorry if this is simple
Ross
I was searching the same thing, and couldn't find the answer anywhere. It turned out to be pretty simple indeed:
ptree pt;
/* load/fill pt */
for(iterator iter = pt.begin(); iter != pt.end(); iter++)
{
std::cout << iter->first << "," << iter->second.data() << std::endl;
}
iter->first
is the entry name, and iter->second.data()
is the entry value of the first level. (You can then re-iterate with iter->second.begin()
/end()
for deeper levels.)
Further, if one such node in this iteration is not a terminal node and is itself a ptree, you can get that as ptree from this iterator itself :
ptree subPt = iter->second.get_child("nodeName");