I don't know about Boost ptree for JSON. I've tried it but it seemed... very clunky.
Here's a simple JSON parser based on the RFC, made in Spirit: https://github.com/sehe/spirit-v2-json/tree/q21356666
You could use it for your use case like test.cpp
#include <vector> #include "json.hpp" struct X { static X from_json(JSON::Value const& v); std::string var1; double var2; }; int main() { auto doc = as_object(JSON::parse( "{\n" " // some values\n" " \"mykey\": [\n" " {\n" " \"var1\": \"value1_str\",\n" " \"var2\" : 3.14\n" " }\n" " ]\n" " // some other values\n" "}\n" )); if (doc.has_key("mykey")) { X data = X::from_json(doc["mykey"]); std::cout << "X.var1: " << data.var1 << "\n"; std::cout << "X.var2: " << data.var2 << "\n"; } std::cout << "doc: " << doc << "\n"; std::cout << "doc[\"mykey\"]: " << doc["mykey"] << "\n"; } X X::from_json(JSON::Value const& v) { X result; auto& o = as_object(as_array(v)[0]); result.var1 = as_string(o["var1"]); result.var2 = as_double(o["var2"]); return result; }
Output:
X.var1: value1_str X.var2: 3.14 doc: {"mykey":[{"var1":"value1_str","var2":3.14}]} doc["mykey"]: [{"var1":"value1_str","var2":3.14}]
There are other json libraries around. I suggest you select one to suit your needs.