ptree 中的 put 操作后可以加 <>, 指定类型, 不加<> 采用默认的类型, 感觉不加反而更好用. 用法见下面例子.
- #include <iostream>
- #include <string>
- #include <boost/property_tree/ptree.hpp>
- #include <boost/property_tree/json_parser.hpp>
- using namespace std;
- using namespace boost::property_tree;
- int main()
- {
- ptree children;
- ptree child1;
- long long i = 2147483648;
- cout <<i << endl;
- children.add_child("数字",child1.put<int>("", i));#put 后面的 <> 可以指定值 i 的类型, 例如此处指定 i 为 int, 当然这里指定 int 会导致溢出. 不指定时, 值 i 默认就是定义的类型 long long.
- write_json("test2.json", children);
- return 0;
- }
相关知识:
boost property_tree 解析 JSON 文件相关文档如下: json_parser ,(ptree 是 basic_ptree<string, string > 的别名)
json_parser:
read_json(filename, ptree): 用于将 filename 文件中的内容读入 ptree 结构中.
write_json(filename, ptree): 用于将 ptree 结构中的内容写入 filename 中.
- basic_ptree:
- self_type& get_child(path_type):
get_value<>: 以某种格式获得某个元素的值. 例子: https://blog.csdn.net/shyanyang/article/details/44203169
来源: http://www.bubuko.com/infodetail-2969395.html