问题描述
我想让write_json
输出一个顶级数组,其效果如下:
I would like to have write_json
output a top level array, something to the effect of:
[{...},{...},{...},...,{...}]
但是当我将列表传递给write_json
时,它会转换为充满空白键的json.
But when I pass a list to write_json
, it converts to a json full of blank keys.
{"":{...},"":{...},"":{...},..."":{...}}
使用add_child
实际上尊重数组,并给我最接近的东西:
Using add_child
actually respects the array and gives me the closest thing of:
{"Some Key":[{...},{...},{...},...,{...}]}
但是那仍然不是我想要的.
But that's still not what I want.
有什么主意如何使该阵列成为顶层?
Any idea how to make that array top level?
推荐答案
Boost没有JSON库(也没有XML库).它具有一个属性树库(恰好包括一个与JSON兼容的表示形式).
Boost does not have a JSON library (nor does it have an XML library). It has a Property Tree library (which happens to include a JSON compatible representation).
您遇到的限制在此处完全清楚地记录在案: http://www.boost.org/doc/libs/1_62_0/doc/html/property_tree/parsers.html#property_tree.parsers.json_parser
The limitations you run into are perfectly clearly documented right there: http://www.boost.org/doc/libs/1_62_0/doc/html/property_tree/parsers.html#property_tree.parsers.json_parser
- JSON对象被映射到节点.每个属性都是一个子节点.
- JSON数组映射到节点.每个元素都是一个空名称的子节点.如果节点同时具有已命名和未命名的子节点,则无法将其映射到JSON表示形式.
- JSON值映射到包含该值的节点.但是,所有类型的信息都会丢失.数字以及文字"null","true"和"false"仅映射到其字符串形式.
- 包含子节点和数据的属性树节点无法映射.
- JSON往返,但类型信息丢失除外.
- 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 round-trips, except for the type information loss.
它继续显示您遇到的确切例子.
It goes on to show an example of EXACTLY what you run into.
这篇关于提升ptree顶层阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!