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 ....`
Lain
  • 2,166
  • 4
  • 23
  • 47
Dhimant Jayswal
  • 123
  • 1
  • 10
  • try sending slightly different json and see if it works: ...,"post":{"source":"1209010340"},... – bobah Nov 29 '12 at 11:40
  • @Lain, please do not label your (several) edits as *Fixed gramatical errors* when what you *really* did was capitalise a few letters. – Sheridan Mar 01 '14 at 00:53
  • @Sheridan How should I label it? I find the letters being lower case very annoying to read. – Lain Mar 01 '14 at 00:53
  • How about *Capitalised letters* like most people put?... they're hardly *gramatical errors*. – Sheridan Mar 01 '14 at 00:54
  • @Sheridan Ok, Sorry. Will change it to that from now on. – Lain Mar 01 '14 at 00:55
  • Of course, it doesn't have to be exactly that... I'm just requesting that you label your edits more accurately. And thanks. – Sheridan Mar 01 '14 at 00:56

2 Answers2

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.

Pedja
  • 186
  • 1
  • 8
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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621