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.
b_d_awould be something like:json j = json::parse(input_string); std::cout << j["b"]["b_d"]["b_d_a"];bis an array. See my answer.j["b"][0]["b_d"][0]["b_d_a"]because bothbandb_dare arrays. I don't know if this syntax is valid though.