C++ 使用 boost 属性树读取 int 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23481262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:27:50  来源:igfitidea点击:

Using boost property tree to read int array

c++jsonboostboost-propertytree

提问by Larry

I have some JSON with a handful of integer array variables, like so:

我有一些带有一些整数数组变量的 JSON,如下所示:

{"a": [8, 6, 2], "b": [2, 2, 1]}

I would like to use boost property_tree, for instance:

我想使用 boost property_tree,例如:

std::stringstream ss;
boost::property_tree::ptree pt;

ss << "{\"a\": [8, 6, 2], \"b\": [2, 2, 1]}";

boost::property_tree::read_json(ss, pt);
std::vector<int> a = pt.get<std::vector<int> >("a");

This doesn't work, nor does any variation on an int pointer that I've tried. How may I read an array from a property tree?

这不起作用,我尝试过的 int 指针也没有任何变化。如何从属性树中读取数组?

采纳答案by sehe

JSON support, is spotty with boost property tree.

JSON 支持,在 boost 属性树方面参差不齐。

The property tree dataset is not typed, and does not support arrays as such. Thus, the following JSON / property tree mapping is used:

  • JSON objects are mapped to nodes. Each property is a child node.
  • 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.
  • JSON values are mapped to nodes containing the value. However, all type information is lost; numbers, as well as the literals "null", "true" and "false" are simply mapped to their string form.
  • Property tree nodes containing both child nodes and data cannot be mapped.

属性树数据集没有类型化,因此不支持数组。因此,使用了以下 JSON / 属性树映射:

  • JSON 对象映射到节点。每个属性都是一个子节点。
  • JSON 数组映射到节点。每个元素都是一个名称为空的子节点。如果节点同时具有命名和未命名的子节点,则无法将其映射到 JSON 表示。
  • JSON 值映射到包含该值的节点。但是,所有类型信息都丢失了;数字以及文字“null”、“true”和“false”都简单地映射到它们的字符串形式。
  • 不能映射同时包含子节点和数据的属性树节点。

(from the documentation)

(来自文档

You can iterate the array with a helper function.

您可以使用辅助函数迭代数组。

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

using boost::property_tree::ptree;

template <typename T>
std::vector<T> as_vector(ptree const& pt, ptree::key_type const& key)
{
    std::vector<T> r;
    for (auto& item : pt.get_child(key))
        r.push_back(item.second.get_value<T>());
    return r;
}

int main()
{
    std::stringstream ss("{\"a\": [8, 6, 2], \"b\": [2, 2, 1]}");

    ptree pt;
    read_json(ss, pt);

    for (auto i : as_vector<int>(pt, "a")) std::cout << i << ' ';
    std::cout << '\n';
    for (auto i : as_vector<int>(pt, "b")) std::cout << i << ' ';
}

See it Live On Coliru. Output:

在 Coliru 上看到它。输出:

8 6 2 
2 2 1

回答by Derek Illchuk

Read your alist as follows:

阅读您的a清单如下:

#include <boost/foreach.hpp>
BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pt.get_child("a.")) {
    cout << v.second.data() << endl;
}