boost生成json中的put操作

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 basic_ptree(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

新战场:https://blog.csdn.net/Stephen___Qin
原文地址:https://www.cnblogs.com/Stephen-Qin/p/10441381.html