[ACCEPTED]-boost : Just iterate over elements of a ptree-boost-propertytree
I was searching the same thing, and couldn't 8 find the answer anywhere. It turned out 7 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 6 name, and iter->second.data()
is the entry value of the first 5 level. (You can then re-iterate with iter->second.begin()
/end()
for 4 deeper levels.)
Further, if one such node 3 in this iteration is not a terminal node 2 and is itself a ptree, you can get that 1 as ptree from this iterator itself :
ptree subPt = iter->second.get_child("nodeName");
I'm having troubles with ptree as well, but 3 perhaps this can help: Check out boost's ptree quick tutorial
v.{entry_name}
would 2 be
v.first
and
v.{value}
v.second.data()
Would 1 that work?
Here's a great example of how to iterate 5 a ptree using BOOST_FOREACH http://akrzemi1.wordpress.com/2011/07/13/parsing-xml-with-boost/
for direct access 4 using the normal "get" functions 3 look at the example from boost: http://www.boost.org/doc/libs/1_51_0/doc/html/boost_propertytree/tutorial.html
the documentation 2 page is located here: http://www.boost.org/doc/libs/1_51_0/doc/html/boost/property_tree/basic_ptree.html I know its not very 1 well documented but it is helpful.
Old thread, but here's a C++11 version of 2 mr_georg's answer with range-based for loops:
ptree pt;
/* load/fill pt */
for(auto pair : pt)
{
std::cout << pair.first << "," << pair.second.data() << std::endl;
}
For 1 this json:
{
"key1":"value1",
"key2":"value2"
}
It outputs:
key1,value1
key2,value2
This example iterates over a simple JSON 1 object and puts its values into a vector.
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
int main (void)
{
try
{
std::stringstream ss;
std::string json_obj_str = "{ \"unit_1\": 1, \"unit_2\": 2, \"unit_3\": 3 }";
std::vector <float> fvec;
ss << json_obj_str; // put string into stringstream
boost::property_tree::ptree pt;
boost::property_tree::read_json(ss, pt); // put stringstream into property tree
// iterate over JSON properties
for (boost::property_tree::ptree::iterator iter = pt.begin(); iter != pt.end(); iter++)
{
std::cout << iter->first << ": " << iter->second.data() << std::endl;
fvec.push_back(boost::lexical_cast<float>(iter->second.data()));
}
for (size_t i = 0; i < fvec.size(); i++)
{
std::cout << "fvec.at(" << i << ") = " << fvec.at(i) << std::endl;
}
}
catch (const boost::property_tree::ptree_error &e)
{
std::cerr << "property_tree error = " << e.what() << std::endl;
return -2;
}
catch (std::exception const& e)
{
std::cerr << "exception = " << e.what() << std::endl;
return -1;
}
return 0;
}
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.