boost property tree not able retrive records after parsing json
Asked Answered
S

2

5

I have a sample json record that I have parsed via boost json parser and saved it to boost property tree to get all key value pairs.ia following code I am able to get first attribute of tree but how can I get second attributes value ? when I try to get it ,it shows me exception that "No such node".

if I iterate the tree ,then it is showing me all keys.I don't understand whats wrong with it. 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 ....`
Stockholder answered 29/11, 2012 at 11:31 Comment(6)
try sending slightly different json and see if it works: ...,"post":{"source":"1209010340"},...Sandy
@Lain, please do not label your (several) edits as Fixed gramatical errors when what you really did was capitalise a few letters.Sicken
@Sicken How should I label it? I find the letters being lower case very annoying to read.Refractometer
How about Capitalised letters like most people put?... they're hardly gramatical errors.Sicken
@Sicken Ok, Sorry. Will change it to that from now on.Refractometer
Of course, it doesn't have to be exactly that... I'm just requesting that you label your edits more accurately. And thanks.Sicken
N
1

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

Norrisnorrv answered 29/11, 2012 at 11:40 Comment(5)
thanks Joachim Pileborg, please suggest me how can i retrive its value ?Stockholder
@DhimantJayswal Either rename it without a dot, or modify the JSON to include the "post" object: ... "post": { "source": ... } ..Norrisnorrv
Json format is fixed for me ,so i cant change the formate of json record.Stockholder
@DhimantJayswal Then you have two options: One is that you talk to the supplier of the JSON data you receive, and tell them that they need to change the name of the structure. The second is that you look for another JSON-parser that can handle names with embedded dots.Norrisnorrv
Such a useless answer, and also accepted. The other answer suggest how to get the valueNoseband
N
7

Since the property name contains a dot, you have to use a different separator, so in your case that would be:

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

Boost documentation section that explains it.

Nakitanalani answered 29/3, 2013 at 15:30 Comment(0)
N
1

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

Norrisnorrv answered 29/11, 2012 at 11:40 Comment(5)
thanks Joachim Pileborg, please suggest me how can i retrive its value ?Stockholder
@DhimantJayswal Either rename it without a dot, or modify the JSON to include the "post" object: ... "post": { "source": ... } ..Norrisnorrv
Json format is fixed for me ,so i cant change the formate of json record.Stockholder
@DhimantJayswal Then you have two options: One is that you talk to the supplier of the JSON data you receive, and tell them that they need to change the name of the structure. The second is that you look for another JSON-parser that can handle names with embedded dots.Norrisnorrv
Such a useless answer, and also accepted. The other answer suggest how to get the valueNoseband

© 2022 - 2024 — McMap. All rights reserved.