Format property tree cannot restore records after json parsing

I have a sample json record that I parsed with a json parser and saved it to expand the property tree to get all the key values ​​of pair.ia in the following code. I can get the first attribute of the tree, but how to get the second attribute of value? when I try to get it, it shows me the exception: "There is no such node."

if I repeat the tree, then it shows me all the keys. I do not understand what is wrong with him. ref: http://www.boost.org/doc/libs/1_52_0/doc/html/boost_propertytree/accessing.html

json string := {"type":"net.aggregate","post.source":"1209010340", "val":1000} 

code:

 boost::property_tree::ptree pt; read_json("jSon string object", pt); cout << pt.get("type", ""); // working cout << pt.get("post.source", "") // showing error ....` 
+4
source share
2 answers

Because Boost property_tree uses a dot to separate different objects. When you request "post.source" , the get function searches for the post object with the source property.

+1
source

Since the property name contains a period, you must use a different separator, so in your case it will be:

 cout << pt.get(ptree::path_type("post.source", '/'), ""); 

Prepare a section of the documentation that explains this .

+7
source

All Articles