如何提升 json 以使用正确的数据类型
how to get boost json to use the correct data types
当我 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 属性 库尚未命名 "Boost Json Library" 因为它不是 JSON 库。相反,它是一个 属性 树库(恰好 使用 JSON 子集用于其目的)。
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.
当我 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 属性 库尚未命名 "Boost Json Library" 因为它不是 JSON 库。相反,它是一个 属性 树库(恰好 使用 JSON 子集用于其目的)。
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.