1

I wan't put to json data in std::unordered_map, for example this:

std::unordered_map<std::string, std::vector<std::string>> data = {
    {"name", {"a", "b", "c", "d", "e"}},
    {"price", {"1.1", "2.2", "3.3", "4.4", "5.5"}},
    {"id", {"1", "2", "3", "4", "5"}}
};

To this json:

[ { "id": "1", "price": "1.1", "name": "a" }, { "id": "2", "price": "2.2", "name": "b" }, { "id": "3", "price": "3.3", "name": "c" }, { "id": "4", "price": "4.4", "name": "d" }, { "id": "5", "price": "5.5", "name": "e" } ]

I tried put this map to boost::propetry_tree::ptree and using write_json function output this in std::cout:

size_t nrows = 5;

boost::property_tree::ptree pt;

for(size_t i = 0; i < nrows; i++) {
    boost::property_tree::ptree body_node;
    for(const auto& kv : data) {
        body_node.put(kv.first, kv.second[i]);
    }
    pt.push_back(std::make_pair("", body_node));
}

boost::property_tree::write_json(std::cout, pt);

I have this output:

{ "": { "id": "1", "price": "1.1", "name": "a" }, "": { "id": "2", "price": "2.2", "name": "b" }, "": { "id": "3", "price": "3.3", "name": "c" }, "": { "id": "4", "price": "4.4", "name": "d" }, "": { "id": "5", "price": "5.5", "name": "e" } }

How I can change that output to expected?

rufous
  • 11
  • 2

0 Answers0