6

I'm trying to use Boost's property tree to parse a JSON-file. Here is the JSON-file

{ "a": 1, "b": [{ "b_a": 2, "b_b": { "b_b_a": "test" }, "b_c": 0, "b_d": [{ "b_d_a": 3, "b_d_b": { "b_d_c": 4 }, "b_d_c": "test", "b_d_d": { "b_d_d": 5 } }], "b_e": null, "b_f": [{ "b_f_a": 6 }], "b_g": 7 }], "c": 8 } 

and a MWE

#include <iostream> #include <fstream> #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/json_parser.hpp> namespace pt = boost::property_tree; using namespace std; int main() { boost::property_tree::ptree jsontree; boost::property_tree::read_json("test.json", jsontree); int v0 = jsontree.get<int>("a"); int v1 = jsontree.get<int>("c"); } 

Question I currently know how to read the outermost variables a and c. However, I'm having difficulty reading other levels such as b_a, b_b_a, b_d_a and so on. How can I do this with Boost? I'm not necessarily looking for a solution involving loops, merely trying to figure out how to "extract" inner variables.

I am open to using other libraries if they are optimal. But so far Boost looks promising to me.

8
  • 1
    For JSON, I'd generally recommend one of two possibilities. If you're doing something like a REST server that needs to operate on massive amounts of JSON as fast as humanly possible, you want RapidJSON. For nearly everything else, you probably want Nlohmann's JSON library. Commented May 1, 2017 at 19:49
  • 1
    Using Nlohmann's library, retrieving b_d_a would be something like: json j = json::parse(input_string); std::cout << j["b"]["b_d"]["b_d_a"]; Commented May 1, 2017 at 20:15
  • 1
    You got the error because I missed the fact that child node b is an array. See my answer. Commented May 1, 2017 at 20:24
  • 1
    @Jerry I think you would need something like j["b"][0]["b_d"][0]["b_d_a"] because both b and b_d are arrays. I don't know if this syntax is valid though. Commented May 1, 2017 at 20:42
  • 1
    @zett42: Oh, quite right (I didn't read the JSON carefully enough). And yes, what you wrote should be fine. Commented May 1, 2017 at 20:44

1 Answer 1

5

To get nested elements you can use the path syntax where each path component is separated by ".". Things are a little bit more complicated here because the child node b is an array. So you can't do without a loop.

const pt::ptree& b = jsontree.get_child("b"); for( const auto& kv : b ){ cout << "b_b_a = " << kv.second.get<string>("b_b.b_b_a") << "\n"; } 

Live demo at Coliru.

I've also added code to print the whole tree recursively so you can see how the JSON gets translated to the ptree. Arrays elements are stored as key/value pairs where the key is an empty string.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.