C:如何使用boost :: property_tree创建数组?

我没有看到一种使用boost ::属性树创建数组的方法.以下代码…

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

#include <iostream>

int main()
{
  try {
    boost::property_tree::ptree props;
    props.push_back(std::make_pair("foo", "bar"));
    props.push_back(std::make_pair("foo", "baz"));
    boost::property_tree::write_json("prob.json", props);
  } catch (const std::exception & ex) {
    std::cout << ex.what() << std::endl;
  }
}

只是给我…

{
  "foo": "bar",
  "foo": "baz"
}

boost :: property_tree上的文档是稀疏的.如何使用boost :: property_tree创建JSON数组?

如果你有一个子树,其唯一的节点有空键,那么它将被序列化为一个数组:

boost::property_tree::ptree array;
array.push_back(std::make_pair("", "bar"));
array.push_back(std::make_pair("", "baz"));

boost::property_tree::ptree props;
props.push_back(std::make_pair("array", array));

boost::property_tree::write_json("prob.json", props);
代码日志版权声明:
翻译自:http://stackoverflow.com/questions/3751357/c-how-to-create-an-array-using-boostproperty-tree

转载注明原文:C:如何使用boost :: property_tree创建数组?