c++ - Boost ptree 顶级数组

标签 c++ json boost boost-propertytree

我想让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/

相关文章:

具有流式传输能力的 Python JSON RPC 服务器

javascript - 创建仅将第一个选项作为标题的下拉列表

c++ - 添加长年持续时间的 Boost.DateTime 问题

c++ - 使用 BOOST Tokenizer 显示定界符而不标记引号中的字符串

c++ - 从函数指针创建 boost::function 的跨平台方式

c++ - 混合赋值和复合赋值运算符 - 优先级?

c++ - STL容器的只读操作

java - GSON : java. lang.IllegalStateException:预期为 BEGIN_OBJECT,但为 BEGIN_ARRAY

c++ - 一劳永逸地理解 C 和 C++ 中 f() 和 f(void) 之间的区别

c++ - 读取存储在多重映射中的私有(private)枚举作为值