boost之ptree学习(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/"}"; 

    ptree pt;

    stringstream stream(s); //这步不知道为什么要这样

    read_json<ptree>( stream, pt); 

    pt.put("study.language.one","physics");//修改数据(这步废了好久时间,最后通过读英文资料解决)

    pt.put("study.fee",600); 

    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 0;

    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()<

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值