boost property_tree解析json文件相关文档如下:json_parserbasic_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的更多相关文章

  1. boost生成json中的put操作

    ptree中的put操作后可以加<>,指定类型,不加<>采用默认的类型,感觉不加反而更好用.用法见下面例子. #include <iostream> #includ ...

  2. Hibernate实体生成JSON的问题及解决

    1.延迟加载所造成的代理对象无法正常序列化的问题 在实体类上添加注解: @JsonIgnoreProperties({ "hibernateLazyInitializer", &q ...

  3. php生成json或者xml数据

    , ,'数据返回成功',$arr);echo $xml;?>

  4. PHP中生成json信息的方法

    <?php //php中生成json信息 //json_encode(数组/对象) $color = array('red','blue','green'); //[索引数组] echo jso ...

  5. php 生成 Json

    php 生成 Json 部分 <?php $arr_result = array(); //返回值 $arr_result['result'] = '0'; $arr_result['calle ...

  6. 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 ...

  7. fastjson生成json时Null属性不显示

    举个例子 生成JSON代码片段 Map < String , Object > jsonMap = new HashMap< String , Object>(); jsonM ...

  8. C#生成JSON数据

    protected void Page_Load(object sender, EventArgs e) { Response.Clear(); Response.ContentType = &quo ...

  9. 生成json对象

    JSONObject 对于放入的object,最终生成的json是什么样的? 两个JavaBean: public class ClassBean { private int grade; priva ...

随机推荐

  1. window的cmd使用

    有时候安装软件也需要使用cmd,如果要进入目前所在盘符的其他路径用cd命令可进入,但如果从c盘进入d盘等,是不用cd命令的. 如目前在c盘的任意目录,需要切换到d盘的根目录,用:“d:”命令(不含引号 ...

  2. Django框架详解

    一.WSGI接口 WSGI服务网关接口:Web Server Gateway Interface缩写. WSGI是python定义的Web服务器和Web应用程序之间或框架之间的通用接口标准. WSGI ...

  3. ajax 获取服务器返回的XML字符串

    前台 解析失败不会抛出任何异常, 只会返回一个给定的错误文档 let l = console.log let http = ajanuw.create({ uri: 'http://localhost ...

  4. 别致的语言GO(GO语言初涉)

    最近由于各种原因(好吧,其实是犯懒)已经许久没有再写新的博文了!最近正好在学习一门新的语言,所以正好记录一下自己的学习成果!最近利用每天晚上下班回来后的几小时,学习了Google开发的Go语言,算是对 ...

  5. 比例尺与DEM空间分辨率

    我国 4 种比例尺 DEM 及其空间分辨率的对应关系:  1:100 万 DEM  --  1000m 空间分辨率:  1:25 万 DEM  --  100m 空间分辨率:  1:5 万 DEM  ...

  6. 正则表达式、re模块

    正则表达式 一说规则我已经知道你很晕了,现在就让我们先来看一些实际的应用.在线测试工具 http://tool.chinaz.com/regex/ 正则表达式是用来匹配字符串非常强大的工具,在其他编程 ...

  7. Flask web开发之路十一

    首先写一下cookie和session的概念,然后是Flask中session的工作机制以及操作session ### cookie: 1. `cookie`出现的原因:在网站中,http请求是无状态 ...

  8. iOS之分类(category)

    1.分类(category)的作用 1.1作用:可以在不修改原来类的基础上,为一个类扩展方法.1.2最主要的用法:给系统自带的类扩展方法. 2.分类中能写点啥? 2.1分类中只能添加“方法”,不能增加 ...

  9. php测试for/while/foreach循环速度对比

    对比代码先行贴上,有疑问或者有不同见解的希望可以提出,大家共同进步: //-------------------------------------$k=0;$checkTime = ['for'=& ...

  10. DEV获取GridControl当前行

    //直接通过gridView获取当前行dr=this.gridView1.GetDataRow(this.gridView1.FocusedRowHandle);//通过DataSet获取数据,需要转 ...