How do I access an JSON array with boost::property_tree? -
{ "menu": { "foo": true, "bar": "true", "value": 102.3e+06, "popup": [ {"value": "new", "onclick": "createnewdoc()"}, {"value": "open", "onclick": "opendoc()"}, ] } }
how can value of onclick?
iterate through children of menu.popup
node , extract onclick
values:
void print_onclick_values(const ptree& node) { boost_foreach(const ptree::value_type& child, node.get_child("menu.popup")) { std::cout << "onclick: " << child.second.get<std::string>("onclick") << "\n"; } }
the function prints:
onclick: createnewdoc() onclick: opendoc()
n.b. delete trailing comma example:
{"value": "open", "onclick": "opendoc()"},
you can't access specific children of array using single get<string>(path)
or get_child(path)
call. the manual says:
depending on path, result @ each level may not determinate, i.e. if same key appears multiple times, child chosen not specified. can lead path not being resolved though there descendant path. example:
a -> b -> c -> b
the path "a.b.c" succeed if resolution of "b" chooses first such node, fail if chooses second.
the elements of json array have empty string name. can access onclick
value array element with
void print_arbitrary_onclick_value(const ptree& node) { std::cout << node.get<std::string>("menu.popup..onclick") << "\n"; }
but don't know element access of onclick
attempted.
Comments
Post a Comment