如何使用json解析器的boost property_tree创建空数组节点 [英] How can I create empty array node using boost property_tree of json parser

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

问题描述

我正在尝试在json中创建一个数组节点,其输出如下所示:

I'm trying to create an array node in json, which output is like this:

{
    node: ["12", "13"]
}

但是当数组为空时,它将输出以下内容:

but when array is empty, it will output this:

{
    node: ""
}

那不是我想要的,我需要这个:

that's not what I want, I need this:

{
    node: []
}

我该怎么做? 而且我不需要在数字前后加上双引号("). 有人可以帮忙吗?

How can I do that ? And I don't need double quotes("") around numbers. Can anyone help ?

我的代码如下:

boost::property_tree::ptree pt;
boost::property_tree::ptree array;
for (vector<int>::const_iterator iter = v.begin();
    iter != v.end();
    ++iter)
{
    boost::property_tree::ptree node;
    node.put("code", *iter);
    array.push_back(std::make_pair("", node));
}
pt.add_child("array", array);

感谢您的关注.

推荐答案

Boost没有JSON库.它具有一个属性树(认为是分层配置格式)库.

Boost doesn't have a JSON library. It has a property-tree (think: hierarchical configuration formats) library.

文档: http: //www.boost.org/doc/libs/1_58_0/doc/html/property_tree/parsers.html#property_tree.parsers.json_parser

它特别指出某些东西没有得到很好的支持:

It specifically states that some things are not well-supported:

  • JSON中的数组是骇客行为(您不能表示空数组)
  • 所有类型的信息都会丢失(所有内容都必须是JSON字符串)

这适合Boost PropertyTree的预期应用程序域.如果它不适合您的问题,请使用JSON库.

This suits the intended application domains for Boost PropertyTree. If it doesn't suit your problem, use a JSON library.

这篇关于如何使用json解析器的boost property_tree创建空数组节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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