C ++: How to create an array using boost :: property_tree?

I see no way to create an array using boost :: property tree. The following code ...

#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/json_parser.hpp> #include <iostream> int main() { try { boost::property_tree::ptree props; props.push_back(std::make_pair("foo", "bar")); props.push_back(std::make_pair("foo", "baz")); boost::property_tree::write_json("prob.json", props); } catch (const std::exception & ex) { std::cout << ex.what() << std::endl; } } 

... just giving me ...

 { "foo": "bar", "foo": "baz" } 

The documents in boost :: property_tree are sparse. How to create a JSON array with boost :: property_tree?

+15
c ++ json boost boost-propertytree
Sep 20 '10 at 12:14
source share
1 answer

If you have a child tree, the only nodes of which have empty keys, then it will be serialized as an array:

 boost::property_tree::ptree array; array.push_back(std::make_pair("", "bar")); array.push_back(std::make_pair("", "baz")); boost::property_tree::ptree props; props.push_back(std::make_pair("array", array)); boost::property_tree::write_json("prob.json", props); 
+20
Sep 20 '10 at 12:59
source share



All Articles