Boost属性树从unamed数组获取元素

时间:2018-05-30 09:47:29

标签: c++ json boost boost-propertytree

我有以下json文件

[  
   {  
      "a":5855925.424944928,
      "b":0,
      "c":96,
      "d":2096640,
      "e":0
   }
]

我这样做,

    boost::property_tree::ptree jsontree;
    std::stringstream ss;
    ss << "[{\"a\": 5855925.424944928, \"b\": 0, \"c\": 96, \"d\": 2096640, \"e\": 0}]";
    boost::property_tree::read_json(ss, jsontree);
    // get the children, i.e. the list elements
    auto bounds = jsontree.equal_range("");
    std::cout << "Size of list : " << std::distance( bounds.first, bounds.second ) << "\n";

但我不知道如何从属性树中读取此内容(例如:get<float>("a")get<int>("c"))?

非常感谢任何帮助。

由于

2 个答案:

答案 0 :(得分:0)

  

注意:实际上不支持顶级数组(你不能使用Boost属性树进行往返)。

     

the documentation

中描述了这些限制      

保存同一棵树将丢失所有类型信息并将数组转换为:

{
    "": {
        "a": "5855925.424944928",
        "b": "0",
        "c": "96",
        "d": "2096640",
        "e": "0"
    }
}

数组是&#34;具有空键的对象&#34;。从你的样本判断,你似乎已经知道了。所以,只需使用它:

for (auto& object_node : boost::make_iterator_range(jsontree.equal_range(""))) {
    ptree const& object = object_node.second;
    std::cout << "a: " << object.get<double>("a") << "\n";
    std::cout << "b: " << object.get<int>("b") << "\n";
    std::cout << "c: " << object.get<int>("c") << "\n";
    std::cout << "d: " << object.get<int>("d") << "\n";
    std::cout << "e: " << object.get<int>("e") << "\n";
}

改进演示

提取某些类型/功能通常更好:

<强> Live On Coliru

#include <iostream>
#include <boost/property_tree/json_parser.hpp>

using boost::property_tree::ptree;

struct Object {
    double a;
    int b, c, d, e;

    friend std::ostream& operator<<(std::ostream& os, Object const& object) {
        return os << "a: " << object.a << "\n"
                  << "b: " << object.b << "\n"
                  << "c: " << object.c << "\n"
                  << "d: " << object.d << "\n"
                  << "e: " << object.e << "\n";
    }

    static Object parse(ptree const& from) {
        return {
            from.get<double>("a"),
            from.get<int>("b"),
            from.get<int>("c"),
            from.get<int>("d"),
            from.get<int>("e"),
        };
    }
};

int main() {
    boost::property_tree::ptree jsontree;
    {
        std::stringstream ss(R"([{"a": 5855925.424944928, "b": 0, "c": 96, "d": 2096640, "e": 0}])");
        boost::property_tree::read_json(ss, jsontree);
    }

    for (auto& object_node : boost::make_iterator_range(jsontree.equal_range(""))) {
        std::cout << Object::parse(object_node.second);
    }
}

打印

a: 5.85593e+06
b: 0
c: 96
d: 2096640
e: 0

答案 1 :(得分:0)

您可以使用BOOST_FOREACH。我附加了一些代码,但它们基于xml_data。但是你可以使用类似的方法在不知道密钥的情况下获取每个值。

 BOOST_FOREACH(pt::ptree::value_type &v, tree) {


    std::string at = v.first + ATTR_SET;

    std::cout << "Extracting attributes from " << at << ":\n";

    BOOST_FOREACH(pt::ptree::value_type &v2,v.second.get_child("<xmlattr>")){
        std::cout<<""<<v2.first.data()<<" : " <<v2.second.data()<<"\n";
    }

    std::cout<<"now in book\n";
    BOOST_FOREACH(pt::ptree::value_type &v3,tree.get_child("bookstore."+v.first)){
        std::cout<<""<<v3.first.data()<<" : " <<v3.second.data()<<"\n";
    }




    m_modules.insert(v.second.data());


}

这里的树是boost :: Property_tree :: ptree