c++ - Parse JSON array value as std::string using Boost -


i'm trying parse std::string json string code:

        std::string ss = "{ \"id\" : \"abc123\", \"number\" : \"0123456789\", \"somelist\" : [{ \"timestamp\" : \"may 1, 2015\" , \"data\" : { \"description\" : \"some description\", \"value\" : \"100.000000\" } }] }";          ptree pt2;         std::istringstream is(ss);         read_json(is, pt2);         std::string _id = pt2.get<std::string>("id");         std::string _number = pt2.get<std::string>("number");         std::string _list = pt2.get<std::string>("somelist");            (auto& e : pt2.get_child("somelist")) {             std::cout << "timestamp: " << e.second.get<std::string>("timestamp") << "\n";             (auto& e1 : pt2.get_child("data")){ // not working                 std::cout << "description: " << e1.second.get<std::string>("description") << "\n";                 std::cout << "value: " << e1.second.get<std::string>("amount") << "\n";             }         } 

although target not print child items (nor convert json string c++ array). code above not work.

i want know how value of data not array or something, string [{ "timestamp" : "may 1, 2015" , "data" : { "description" : "some description", "value" : "100.000000" } }]

just need json array as-is std::string

boost/property_tree/json_parser.hpp implements write_json, is, expect, inverse of read_json. since ptree stores arrays objects empty key, achieve representation want, loop on top-level ptrees in pt2.get_child("somelist"), call write_json on each of them , format these representations wish.

for(auto const& node : pt2.get_child("somelist"))  write_json(std::cout, node.second); 

coliru demo.


Comments