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中提到,最近在写一个很简单的天气预报应用.即使功能很简单,但我还是想把它做成一个相对完 ...
随机推荐
- anroid studio 找不到符号 位置: 类 drawable
一般是把图片加到mipmap之后写成R.drawable.xxx引起的
- git config配置文件
设置 git status的颜色. git config --global color.status auto 一.Git已经在你的系统中了,你会做一些事情来客户化你的Git环境.你只需要做这些设置一 ...
- C#文件和文件文件夹按时间、名称排序-顺序与倒序
对于文件和文件夹有多种排序方式,常用的就是按创建或修改时间.按文件名排序.在 C# 中,按时间和文件名排序都十分简单,用数组提供的排序方法 Array.Sort() 一行代码就可以搞定,当然也可以用常 ...
- C#报错:创建调试信息文件 ……obj\Debug\model.pdb: 拒绝访问
错误:创建调试信息文件“.......\obj\Debug\model.pdb”时发生错误 --“......\obj\Debug\model.pdb: 拒绝访问. 解决办法如下: 删除该项目下的 b ...
- linux修改网卡名称
本文转载自江一<linux修改网卡名称> 终端输入:vi /etc/udev/rules.d/70-persistent-net.rules 出现以下文件 # This file was ...
- Java运算符优先级
序列号 符号 名称 结合性(与操作数) 目数 说明 1 . 点 从左到右 双目 ( ) 圆括号 从左到右 [ ] 方括号 从左到右 2 + 正号 从右到左 单目 - 负号 从右到左 单目 ++ ...
- [CareerCup] 8.7 Chat Server 聊天服务器
8.7 Explain how you would design a chat server. In particular, provide details about the various bac ...
- 信息安全系统设计基础_exp3
北京电子科技学院(BESTI) 实 验 报 告 课程:信息安全系统设计基础 班级:1353 姓名:吴子怡.郑伟 学号:20135313.20135322 指导教师: 娄嘉鹏 实验 ...
- Javascript设计模式之我见:迭代器模式
大家好!本文介绍迭代器模式及其在Javascript中的应用. 模式介绍 定义 提供一种方法顺序一个聚合对象中各个元素,而又不暴露该对象内部表示. 类图及说明 Iterator抽象迭代器 抽象迭代器负 ...
- Java并发之:生产者消费者问题
生产者消费者问题是Java并发中的常见问题之一,在实现时,一般可以考虑使用juc包下的BlockingQueue接口,至于具体使用哪个类,则就需要根据具体的使用场景具体分析了.本文主要实现一个生产者消 ...