Parse JSON array as std::string with Boost ptree

前端 未结 3 1598
花落未央
花落未央 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: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 
    #include 
    
    #include 
    
    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("a") << std::endl;
    }
    

提交回复
热议问题