boost生成json
boost property_tree解析json文件相关文档如下:json_parser、basic_ptree
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
自己的实践:
#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 pt;
ptree children;
ptree child1, child2, child3; child1.put("", );
child2.put("", );
child3.put("", );
//write_json("test2.json", child1); //put空字符串后,马上转json会报错。 children.push_back(std::make_pair("", child1));
children.push_back(std::make_pair("", child2));
children.push_back(std::make_pair("", child3));
write_json("test3.json", children); //此时就不会报错. 此时不会生成数组. pt.add_child("MyArray", children); //add_child操作之后才生成数组的.如果要生成[]嵌套[],需要在外层再进行一次ptOutArray.push_back(make_pair("", ptInnerArray)),最后再进行add_child操作. write_json("test1.json", pt);
return ;
}
test1.json内容:
{
"MyArray": [
"1",
"2",
"3"
]
}
test3.json内容:
{
"": "1",
"": "2",
"": "3"
}
另外的说明演示.
int main()
{
ptree out;
ptree pt;
ptree children;
ptree child; for (int i = ; i < ; i++)
{
child.put("", i);
// children.push_back(std::make_pair("", child));
children.put_child("", child);//add_child,put_child的key不能为空;而make_pair可以加入空值.
} write_json("test3.json", children); //此时就不会报错 // pt.push_back(make_pair("stock_pool", children)); //当key不为空时,add_child和push_back(make_pair())的效果是一样的.(除了我知道的键里含"."的情况,见下面的例子)
pt.put_child("stock_pool", children);
pt.add_child("stock_pool", children); // out.push_back(std::make_pair("data", pt));
out.put_child("data", pt);
// out.put_child("data", pt);//put操作时,key一样时只能不能够再添加上去,而add_child没有这个限制.
ostringstream os;
write_json(os, out);
cout << os.str() << endl;
write_json("test1.json", pt);
return ;
}
property_tree中的put( , ),push_back(make_pair( , ))中的第一个参数可以为空字符串;而put_child( , ),add_child( , )的第一个参数不能为空字符串.
在进行add_child操作时,如果键中有"."的话,会割裂开来.比如键为"he.llo"时,此时,需要用push_back来替代add_child了.
{
"he": {
"llo": [
[
"",
"",
""
]
]
}
}
boost生成json的更多相关文章
- boost生成json中的put操作
ptree中的put操作后可以加<>,指定类型,不加<>采用默认的类型,感觉不加反而更好用.用法见下面例子. #include <iostream> #includ ...
- Hibernate实体生成JSON的问题及解决
1.延迟加载所造成的代理对象无法正常序列化的问题 在实体类上添加注解: @JsonIgnoreProperties({ "hibernateLazyInitializer", &q ...
- php生成json或者xml数据
, ,'数据返回成功',$arr);echo $xml;?>
- PHP中生成json信息的方法
<?php //php中生成json信息 //json_encode(数组/对象) $color = array('red','blue','green'); //[索引数组] echo jso ...
- php 生成 Json
php 生成 Json 部分 <?php $arr_result = array(); //返回值 $arr_result['result'] = '0'; $arr_result['calle ...
- PHP“Cannot use object of type stdClass as array” (php在调用json_decode从字符串对象生成json对象时的报错)
php再调用json_decode从字符串对象生成json对象时,如果使用[]操作符取数据,会得到下面的错误 错误:Cannot use object of type stdClass as arra ...
- fastjson生成json时Null属性不显示
举个例子 生成JSON代码片段 Map < String , Object > jsonMap = new HashMap< String , Object>(); jsonM ...
- C#生成JSON数据
protected void Page_Load(object sender, EventArgs e) { Response.Clear(); Response.ContentType = &quo ...
- 生成json对象
JSONObject 对于放入的object,最终生成的json是什么样的? 两个JavaBean: public class ClassBean { private int grade; priva ...
随机推荐
- shell参数扩展
http://zuyunfei.com/2016/03/23/Shell-Truncate-File-Extension/
- atoi函数的用法
库函数原型: #inclue <stdlib.h> int atoi(const char *nptr); 用法:将字符串里的数字字符转化为整形数.返回整形值. 注意:转化时跳过前面的空格 ...
- [No0000189]改善C#程序的建议10:用Parallel简化Task
在命名空间System.Threading.Tasks下,有一个静态类Parallel简化了在同步状态下的Task的操作.Parallel主要提供了3个有用的方法:For.ForEach.Invoke ...
- webkit下面的CSS设置滚动条
webkit下面的CSS设置滚动条 1.主要有下面7个属性: ::-webkit-scrollbar 滚动条整体部分,可以设置宽度啥的 ::-webkit-scrollbar-button 滚动条两端 ...
- shell之使用paste命令按列拼接多个文件
试验文件: [root@db03 shell-script]# cat text1.txt 1 2 3 4 5 [root@db03 shell-script]# cat text2.txt orac ...
- Exception 07 : org.hibernate.LazyInitializationException: could not initialize proxy - no Session
异常名称: org.hibernate.LazyInitializationException: could not initialize proxy - no Session 异常截图: 异常详情: ...
- mongodb操作文件
mongodb操作文件,主要是通过GridFS类.存储文件主要存放在fs中,其中的fs是数据库默认的.并且GridFS是直接与数据库打交道,与collection集合无关. ============= ...
- python知识点杂记
1. Python没有 ++, --操作. 2. Join比+快:tuple比list快 3. Dict的key是区分大小写的 4. 参数顺序:无默认值参数,有默认值参数,tuple,dict 5. ...
- sql server创建临时表的两种写法和删除临时表
--创建.删除临时表 --第一种方式 create table #tmp(name varchar(255),id int) --第二种方式 select count(id) as storyNum ...
- 《Mysql DML语句》
1:DISTINCT 用于去重,但是需要注意的是,它是用于所有列的,也就是说,除非指定的列全部相同,否则所有的行都会被检索出来. 2:ORDER BY 用于排序,但是应该注意的是,它因该是 SELEC ...