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中提到,最近在写一个很简单的天气预报应用.即使功能很简单,但我还是想把它做成一个相对完 ...
随机推荐
- [资料]Nginx做IP访问限制以及正则规则
nginx配置location总结及rewrite规则写法 Nginx Location配置总结 Nginx 禁止某个IP访问 server { listen 443; root /webroot/; ...
- C语言 结构体中属性的偏移量计算
//计算结构体偏移量 #include<stdio.h> #include<stdlib.h> #include<string.h> //详解:对于offscfof ...
- 职业卖家淘宝美工教程,掌握技能无师自通 学到的不只是PS(共81节)附素材【无水印版】
职业卖家淘宝美工教程,掌握技能无师自通 学到的不只是PS(共81节)附素材[无水印版]设计传说出品的专业课程是我们资深培训讲师精心录制的,只有视频教程和常用必备的插件,其他绝不掺和,如果你是职业卖家, ...
- 启动页面设置,icon图标设置
更多尺寸像素如何放置请看:http://chicun.jammy.cc/ 如何设置App的启动图,也就是Launch Image? 新建一个iosLaunchImage文件夹
- poj1067-取石子游戏-wythoff博弈
打表找规律失败,搜了一下原来是wythoff博弈 /*------------------------------------------------------------------------- ...
- 【原创】基于Memcached 实现用户登录的Demo(附源码)
一个简单的Memcached在Net中运用的一个demo.主要技术 Dapper+MVC+Memcached+sqlserver, 开发工具为vs2015+Sql 效果图如下: 登录后 解决方案 主要 ...
- IT男的”幸福”生活"续4
翻来翻去,总是睡不觉.大脑口一堆问题.一个又冒出一个,没完没了.明天该怎样去进行下一步呢.. ….. 夜一下子深黑很多,窗外的公路,时而有货车通过,动不动按喇叭,而我住在二楼,真它的吵.也许她住在五楼 ...
- js1常用的东西
1 .ready 与resize方法.$(inject).ready(function() { var windowWidth = $(document.body).outerWidth(true); ...
- json和cookie兼容以前的
'json': function(data) { try { if (typeof data === "string") { if (typeof JSON != 'undefin ...
- 07.C#泛型的限制和可空类型的简单说明(三章3.5-四章4.1)
自己在写文章的同时,也是在学习,对于书中的语句很多其实没有太好的理解,读一本书,要消化!!!三章都是讲泛型的,最后写一下泛型的限制,对于本章学习的完结,one end,one begin. 看下面的代 ...