c++ - 为什么 boost::property_tree::write_json() 将整数值转换为字符串?这是不正确的。

标签 c++ json boost

我的输入:data.txt

{
        "target_url":"www.19lou.com/forum-1637-thread-10031471311655793-1-1.html",
        "trespassing_field":{
            "ratetype":5,
            "spider":{
                "prod_name":"name",
                "link_src":1 
            }
        }
}

使用代码:

boost::property_tree::ptree json_tree;
boost::property_tree::read_json("data.txt", json_tree);
std::stringstream json_main_pack;
boost::property_tree::write_json(json_main_pack, json_tree);
LOG(NOTICE) << "json: " << json_main_pack.str();

输出:

{
    "target_url": "www.19lou.com\/forum-1637-thread-10031471311655793-1-1.html",
    "trespassing_field": {
        "ratetype": "5",
        "spider": {
            "prod_name": "name",
            "link_src": "1"
        }
    }
}

write_json() 将整数值转换为字符串。它将"ratetype":5 转换为"ratetype": "5"。这是不正确的。 如何准确转换?输入整数值,然后输出整数值。

最佳答案

来自 Boost.PropertyTree 文档 here ,看起来所有类型信息都丢失了。相关引用:

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.

强调我的。不幸的是,如果您想保留类型信息,您可能需要使用不同的 JSON 解析器。

关于c++ - 为什么 boost::property_tree::write_json() 将整数值转换为字符串?这是不正确的。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51857285/

相关文章:

c++ - cmake 链接 libboost_python-py32.so 而不是 libboost_python.so

c++ - 很难理解封装

c++ - 为什么 linux 在分配动态内存时会占用一些额外的空间?

json - 在 ASP.Net MVC 中设置 Access-Control-Allow-Origin - 最简单的方法

jquery菜鸟: cannot parse json

android - 将图像加载到 Imageview

c++将结构 append 到二进制文件中

c++ - 多重映射迭代器是否也需要排序函数的类型?

C++,从函数返回字符串; boost::asio 读/写

c++ - 使用Google Test对自定义Assert函数进行单元测试