boost之ptree學習(json)

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.