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://www.cnblogs.com/Stephen-Qin/p/10441381.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值