Boost解析JSON格式

來源:互聯網
上載者:User

解析Json的方法有很多,也有不少的第三方開源工具。這裡僅介紹其中的一種,用Bosst解析。Boost庫是一個可移植、提供原始碼的C++庫,作為標準庫的後備,是C++標準化進程的發動機之一。 Boost庫由C++標準委員會庫工作群組成員發起,其中有些內容有望成為下一代C++標準庫內容。在C++社區中影響甚大,是不折不扣的“准”標準庫。Boost由於其對跨平台的強調,對標準C++的強調,與編寫平台無關。大部分boost庫功能的使用只需包括相應標頭檔即可,少數(如Regex庫,檔案系統庫等)需要連結庫。但Boost中也有很多是實驗性質的東西,在實際的開發中實用需要謹慎。

鑒於Boost的強大功能,就用Boost來解析Json格式,包括簡單的及複雜的。

首先給出一個Json例子。

{ "people": [ { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },   

{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb"},   

{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }   ]} 

要解析Json,需要包括標頭檔。

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/date_time.hpp>

還有

#include <string>
#include <vector>

#include <sstream.h>

using namespace boost::property_tree;
using namespace boost::gregorian;
using namespace boost;

接著,將上面這串Json賦給一個變數

string strJson ={ "people": [ { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },  

{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb"},   

{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }   ]}

註:在C++中需要在“前面加入\進行轉意。

接下來,給程式增加如下變數:

string stra,strc;
vector<string> vecStr;
ptree pt,p1,p2;
stringstream stream;

下面的程式是解析Json

    stream << strJson;
    read_json<ptree>( stream, pt);
    p1 = pt.get_child("people");
    for (ptree::iterator it = p1.begin(); it != p1.end(); ++it)
    {
        p2 = it->second; //first為空白
        stra = p2.get<string>("firstName");
        vecStr.push_back(stra);  
    }

這樣,vecStr中就有三個值,Brett,Jason,Elliotte,Json解析完成。

對於下面這樣的Json格式,

{ "programmers": [ { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },   { "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },   { "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }  ],  "authors": [   { "firstName":
"Isaac", "lastName": "Asimov", "genre": "science fiction" },   { "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },   { "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }   ],   "musicians": [   { "firstName": "Eric",
"lastName": "Clapton", "instrument": "guitar" },   { "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }   ] } 

就需要對上面的這兩句程式稍微改動下就行了。

 p1 = pt.get_child("programmers");

stra = p2.get<string>("firstName");

對比一下,發現什麼不同了嗎?就是將參數稍微修改了一下。如果要得到musicians裡面的firstName呢?就只要將p1 = pt.get_child("programmers");中的參數改成musicians;

如果Json裡面嵌套了Json,則需要增加一個Ptree 變數pt3,pt4.使

p3 = p2.get_child("嵌套的值");

for (ptree::iterator ita = p3.begin(); ita != p3.end(); ++ita)
         {
             p4 = ita->second;
             strc = p4.get<string>("markerItemLink");            
         }

相關原始碼請訪問地址

http://download.csdn.net/detail/yqmfly/3729591

有問題可以留言。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.