C ++:如何创建使用boost :: property_tree一个数组? [英] C++: How to create an array using boost::property_tree?

查看:1688
本文介绍了C ++:如何创建使用boost :: property_tree一个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白的方式来创建使用boost ::属性树的数组。下面code ...

I don't see a way to create an array using boost::property tree. The following code ...

#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;
  }
}

...只是给了我...

... just gives me ...

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

上的boost :: property_tree该文档是稀疏。如何创建一个JSON数组的boost :: property_tree?

The docs on boost::property_tree are sparse. How do I create an JSON array with boost::property_tree?

推荐答案

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

If you have a sub-tree whose only nodes have empty keys, then it will be serialized as an array:

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);

这篇关于C ++:如何创建使用boost :: property_tree一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆