Parse JSON array as std::string with Boost ptree

前端 未结 3 1518
花落未央
花落未央 2020-12-10 18:03

I have this code that I need to parse/or get the JSON array as std::string to be used in the app.

std::string ss = \"{ \\\"id\\\" : \\\"123\\\", \\\"number\         


        
相关标签:
3条回答
  • 2020-12-10 18:26

    Arrays are represented as child nodes with many "" keys:

    docs

    • JSON arrays are mapped to nodes. Each element is a child node with an empty name. If a node has both named and unnamed child nodes, it cannot be mapped to a JSON representation.

    Live On Coliru

    #include <boost/property_tree/ptree.hpp>
    #include <boost/property_tree/json_parser.hpp>
    
    using boost::property_tree::ptree;
    
    int main() {
        std::string ss = "{ \"id\" : \"123\", \"number\" : \"456\", \"stuff\" : [{ \"name\" : \"test\" }, { \"name\" : \"some\" }, { \"name\" : \"stuffs\" }] }";
    
        ptree pt;
        std::istringstream is(ss);
        read_json(is, pt);
    
        std::cout << "id:     " << pt.get<std::string>("id") << "\n";
        std::cout << "number: " << pt.get<std::string>("number") << "\n";
        for (auto& e : pt.get_child("stuff")) {
            std::cout << "stuff name: " << e.second.get<std::string>("name") << "\n";
        }
    }
    

    Prints

    id:     123
    number: 456
    stuff name: test
    stuff name: some
    stuff name: stuffs
    
    0 讨论(0)
  • 2020-12-10 18:43

    continue with @sehe 's answer, related to the question asked by @xkm

    Is it possible to get it like '[{ "name" : "some" }, { "name" : "stuffs" }]'

    Yes, you can. Just treat it with an "unnamed" key which means the key with empty string.

    f.g.

    #include <boost/property_tree/ptree.hpp>
    #include <boost/property_tree/json_parser.hpp>
    
    #include <iostream>
    
    using boost::property_tree::ptree;
    
    int main()
    {
        std::stringstream ss;
        ss << R"([{"a": 5}, {"a": 9}])";
    
        ptree pt;
        read_json(ss, pt);
        for (auto& item : pt.get_child(""))
           std::cout << "value is " << item.second.get<int>("a") << std::endl;
    }
    
    0 讨论(0)
  • 2020-12-10 18:45

    Since "stuff" is an array, you can iterate over the elements of it, which are dictionaries. And then you can iterate over the elements of the dictionary, which are key-value pairs:

    for (const auto& dict : pt2.get_child("stuff")) {
        for (const auto& kv : dict.second) {
            std::cout << "key = " << kv.first << std::endl;
            std::cout << "val = " << kv.second.get_value<std::string>() << std::endl;
        }
    }
    
    0 讨论(0)
提交回复
热议问题