我想让write_json
输出一个顶级数组,其效果是:
[{...},{...},{...},...,{...}]
但是当我将列表传递给 write_json
时,它会转换为充满空白键的 json。
{"":{...},"":{...},"":{...},..."":{...}}
使用 add_child
实际上尊重数组并给我最接近的东西:
{"Some Key":[{...},{...},{...},...,{...}]}
但这仍然不是我想要的。
知道如何使该数组成为顶级吗?
最佳答案
Boost 没有 JSON 库(也没有 XML 库)。它有一个属性树库(恰好包含 JSON 兼容表示)。
您遇到的限制在这里有非常清楚的记录:http://www.boost.org/doc/libs/1_62_0/doc/html/property_tree/parsers.html#property_tree.parsers.json_parser
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 round-trips, except for the type information loss.
它继续展示了您遇到的确切情况的示例。
关于c++ - Boost ptree 顶级数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40292925/