boost解析json
#include <QtCore/QCoreApplication>
#include <boost/property_tree/ptree.hpp>
#include <boost/typeof/typeof.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <iostream>
using namespace std;
using namespace boost::property_tree;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
string s = "{/"age/" : 26,/"study/":{/"language/":{/"one/":/"chinese/",/"two/":/"math/"},/"fee/":500,/"subject/":[{/"one/":/"china/"},{/"one/":/"Eglish/"}]},/"person/":[{/"id/":1,/"name/":/"chen/"},{/"id/":2,/"name/":/"zhang/"}],/"name/" : /"huchao/"}";
string s = "{age : 26,study:{
language:{one:chinese,two:math},
fee:,
subject:[{one:china},{one:Eglish}]
},
person:[{id:,name:chen},{id:,name:zhang}],
name : huchao}";
ptree pt;
stringstream stream(s); //这步不知道为什么要这样
read_json<ptree>( stream, pt);
pt.put("study.language.one","physics");//修改数据(这步废了好久时间,最后通过读英文资料解决)
pt.put("study.fee",);
string s1=pt.get<string>("age");
cout<<s1<<endl;
string s2=pt.get<string>("name");
cout<<s2<<endl;
string s3=pt.get_child("study").get_child("language").get<string>("one");
cout<<s3<<endl;
string s4=pt.get_child("study").get<string>("fee");
cout<<s4<<endl;
ptree p1,p2;
p1 = pt.get_child("study").get_child("subject");//访问多级节点中的数组数据
for (ptree::iterator it = p1.begin(); it != p1.end(); ++it)
{
p2 = it->second; //first为空
cout<<"subject="<<p2.get<string>("one")<<endl;
}
return ;
return a.exec();
}
// pt.put("conf.theme", "Matrix Reloaded");
// pt.put("conf.clock_style", 13);
// pt.put("conf.gui", 0);
// pt.put("conf.urls.url","http://www.url4.org");
// pt.add("conf.urls.url","http://www.url4.org");
// write_json("conf.json", pt);
// read_json("conf.json",pt);
// cout<< pt.get<string>("conf.theme") <<endl;
// cout<< pt.get<int>("conf.clock_style") <<endl;
// cout<< pt.get<long>("conf.gui") <<endl;
// cout<< pt.get("conf.no_prop", 100) <<endl;
// BOOST_AUTO(child, pt.get_child("conf.urls"));
// for(BOOST_AUTO(pos,child.begin()); pos != child.end(); ++pos)
// {
// cout<<pos->second.data()<<",";
// }
// cout<<endl;
// ptree pt_1,pt_11,pt_12;
// pt_11.put("id","3445");
// pt_11.put<int>("age",29);
// pt_11.put("name","chen");
// pt_12.push_back(make_pair("",pt_11));
// pt_12.push_back(make_pair("",pt_11));
// //replace or create child node "data"
// pt_1.put_child("data",pt_12);
// ostringstream os;
// write_json(os,pt_1);
// cout<<os.str()<
里面的有些还是比较模糊,原理不清
vs提示个错误:pnode.get<int>("x")不存在从 "char [2]" 转换到 "boost::property_tree::string_path<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,boost::property_tree::id_translator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >" 的适当构造函数
查了半天结果可以运行,这VS也有点坑人吧
boost解析json的更多相关文章
- boost解析json(2)
"{ "A":1, "B":{ "C":2, "D":3 }, "E":[ {" ...
- Boost property_tree解析json
使用Boost property_tree解析json 之前使用jsoncpp解析json,现在才知道boost就有解析的库,学习一下吧 property_tree可以解析xml,json,ini,i ...
- Boost.JSON Boost的JSON解析库(1.75首发)
目录 目录 Boost的1.75版本新库 JSON库简介 JSON的简单使用 编码 最通用的方法 使用std::initializer_list json对象的输出 两种对比 解码 简单的解码 增加错 ...
- 更好更快更高效解析JSON说明
现在来一个实例解析类,直接就把解析JSON到QVariant去了.唯一不足的是没有搞错误处理,具体方法也请各位自行参考json-c的发行文档,这样比较方便叙述,STL或者Boost我都没有认真接触过, ...
- boost生成json中的put操作
ptree中的put操作后可以加<>,指定类型,不加<>采用默认的类型,感觉不加反而更好用.用法见下面例子. #include <iostream> #includ ...
- boost生成json
boost property_tree解析json文件相关文档如下:json_parser.basic_ptree json_parser:read_json(filename, ptree):用于将 ...
- Xamarin.Android下获取与解析JSON
一.新建项目 1.新建一个Android项目,并命名为为NetJsonList 2.右击引用,选择添加引用,引用System.Json.dll 二.同步请求 既然是跨平台,我们自然不能按照java下的 ...
- 使用Newtonsoft.Json.dll(JSON.NET)动态解析JSON、.net 的json的序列化与反序列化(一)
在开发中,我非常喜欢动态语言和匿名对象带来的方便,JSON.NET具有动态序列化和反序列化任意JSON内容的能力,不必将它映射到具体的强类型对象,它可以处理不确定的类型(集合.字典.动态对象和匿名对象 ...
- 阶段一:通过网络请求,获得并解析JSON数据(天气应用)
“阶段一”是指我第一次系统地学习Android开发.这主要是对我的学习过程作个记录. 在上一篇阶段一:解析JSON中提到,最近在写一个很简单的天气预报应用.即使功能很简单,但我还是想把它做成一个相对完 ...
随机推荐
- 【WPF】WPF通过RelativeSource绑定父控件的属性
1.后台代码实现绑定父控件的属性 RelativeSource rs = new RelativeSource(RelativeSourceMode.FindAncestor); //设定为离自己控件 ...
- zabbix架构
监控系统: 数据的采集---存储---展示和分析---报警 snmp cacti:rrd agent nagios:不保存数据,mysql icmp/ssh ...
- Django基础 - Debug设置为False后静态文件获取404
当设置setting.py文件当中的DEBUG=FALSE后,Django会默认使用Web Server的静态文件处理,故若没设置好Web Server对静态文件的处理的话,会出现访问静态文件404的 ...
- pandas 练习
from pandas import Series, DataFrame # Series接收list或dict作为一维数据 #两个属性:values, index #① s1 = Series([4 ...
- Python之socket(套接字)
Socket 一.概述 socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. ...
- LeetCode:Word Break(DP)
题目地址:http://oj.leetcode.com/problems/word-break/ 简单的动态规划问题,采用自顶向下的备忘录方法,代码如下: class Solution { publi ...
- 20135220谈愈敏Blog6_进程的描述和创建
进程的描述和创建 谈愈敏 原创作品转载请注明出处 <Linux内核分析>MOOC课程 http://mooc.study.163.com/course/USTC-1000029000 进程 ...
- 证书与keytool
证书的来源与使用: 对数据进行签名是我们在网络中最常见的安全操作.签名有双重作用,作用一就是保证数据的完整性,证明数据并非伪造,而且在传输的过程中没有被篡改,作用二就是防止数据的发布者否认其发布了该数 ...
- Kindeditor(JSP)& 上传目录不存在
一.基本配置 Kindeditor是一款富文本编辑器,作用跟博客园写文章用的这个编辑器一样,都是为了让文本加入各种各样的修饰元素. 使用方法如下,解压缩,把期中的ASP\NET\PHP文件夹都删除,然 ...
- 附加到iis进程调试时找不到w3wp.exe
在进程列表的下面,有个show processes in all sessions,把它勾上就能看到了