我的输入: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/