【问题标题】:Boost property tree getting element from unamed array提升属性树从未命名数组中获取元素
【发布时间】:2018-05-30 09:47:29
【问题描述】:

我有以下 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&lt;float&gt;("a")get&lt;int&gt;("c"))?

任何帮助将不胜感激。

谢谢

【问题讨论】:

  • 您有一个包含单个元素的“数组”。这个单一元素是一个“对象”。首先需要从数组中获取这个单一的对象元素,然后才能从对象中获取属性。

标签: c++ json boost boost-propertytree


【解决方案1】:

注意:实际上不支持顶级数组(您不能使用 Boost Property Tree 来回往返)。

限制在the documentation中描述

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

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

数组是“具有空键的对象”。从您的样本来看,您似乎已经知道这一点。所以,就用它吧:

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

【讨论】:

    【解决方案2】:

    您可以为此使用 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-27
      • 2020-08-06
      • 1970-01-01
      相关资源
      最近更新 更多