gpt4 book ai didi

c++ - 如何让 boost json 使用正确的数据类型

转载 作者:太空狗 更新时间:2023-10-29 20:39:03 24 4
gpt4 key购买 nike

当我 put_value 使用 int 时,它被写为字符串。有谁知道如何让它作为 int 打印?

#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

using boost::property_tree::ptree;
using namespace std;

int main(int argc, char* argv[]) {

ptree node;
node.put("string", "text here");
node.put("int", 1);//outputs as "1" and should be 1
write_json(cout, node, false);//{"string":"text here","int":"1"}

return 0;
}

最佳答案

图书馆明确表示不支持它。

Boost Property Library 尚未命名为“Boost Json Library”,因为它不是 JSON 库。相反,它是一个属性树库(恰好使用 JSON 子集用于其目的)。

来自documentation :

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 json 使用正确的数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29243662/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com