C++解析JSON(jsonCpp)

來源:互聯網
上載者:User

標籤:json   c++   

C++解析JSON(jsonCpp)

JSON(JavaScript Object Notation) 是一種輕量級的資料交換格式。它基於JavaScript(Standard ECMA-262 3rd Edition - December 1999)的一個子集。 JSON採用完全獨立於語言的文字格式設定,但是也使用了類似於C語言家族的習慣(包括C, C++, C#, Java, JavaScript, Perl, Python等)。這些特性使JSON成為理想的資料交換語言。 易於人閱讀和編寫,同時也易於機器解析和產生(網路傳輸速度)
這裡我們主要使用jsoncpp來解析json檔案的

準備工作下載jsonCpp

我們首先建立一個檔案夾jsonCpp,然後將jsoncpp複製到我們的檔案夾下面:

>mkdir jsonCpp && cd jsonCpp
>git clone https://github.com/open-source-parsers/jsoncpp.git

現在你的檔案夾下面就會有一個jsoncpp檔案夾,這裡面就是jsoncpp的源碼

編譯jsoncpp

面對這個多個檔案和檔案夾,是不是有點混亂了,但是我們用到的東西就只有兩個檔案夾,一個是src/lib_json檔案夾,一個是include/json檔案夾

於是建立一個目錄,並將這兩個檔案夾複製出來,將json檔案夾複製到lib_json裡面

下載我們就需要編譯這些檔案,並產生靜態連結庫,其實不編譯也行,只是後面用起來方便一點,我們來編輯一個Makefile

TARGET=libjson.libSRCS=json_writer.cpp     json_reader.cpp     json_value.cppOBJS=$(SRCS:.cpp=.o)INCS=-I json$(TARGET):$(OBJS)    ar rv [email protected] $^%.o:%.cpp    g++ -c $< $(INCS) -o [email protected]

make 以後發現報錯了

顯示錯誤為:

報錯找不到標頭檔
博主琢磨了一番,發現還是找不到解決方案(對makefile不太熟悉,有大神指點一下嗎?),只有改源檔案了了,於是將json_value.cppjson_reader.cpp,json_write.cpp這三個檔案裡面的標頭檔的角括弧改為雙引號,然後make編譯成功。

讀取JSON檔案

我們得到了jsonCpp的靜態連結庫libjson.lib,接下面我們開始用jsoncpp解析json檔案了
從上面的編譯可知,jsoncpp裡面大致包含三個類:

  • Json::Value 用來儲存讀取的資料或者將要寫入檔案的資料
  • Json::Reader 用來讀取JSON檔案裡面的資料,並傳入到Json::Value對象裡面去
  • Json::FastWriter 用來將Json::Value對象寫入到一個JSON檔案中去
讀取簡單的JSON檔案
include <fstream>#include <cassert>#include "json/json.h"#include <iostream>using namespace std;int main(){    ifstream inFile("test.json", ios::in);    if(!inFile.is_open())    {        cerr<<"Open the file failed"<<endl;        return -1;    }    Json::Value root;    Json::Reader reader;    if(!reader.parse(inFile , root , false))    {        cerr<<"parse file failed"<<endl;        return -1;    }    cout<<"name : "<<root["name"].asString()<<endl;    cout<<"age : "<<root["age"].asInt()<<endl;    return -1;}

test.json裡面的資料為:

{    "name":"qeesung",    "age":21}

我們編譯一下:g++ main.cpp libjson.lib -o myJson
運行結果為:

讀取含有數組的JSON檔案
#include <fstream>#include <cassert>#include "json/json.h"#include <iostream>using namespace std;int main(){    ifstream inFile("test.json", ios::in);    if(!inFile.is_open())    {        cerr<<"Open the file failed"<<endl;        return -1;    }    Json::Value root;    Json::Reader reader;    if(!reader.parse(inFile , root , false))    {        cerr<<"parse file failed"<<endl;        return -1;    }    for(int i = 0 ; i<root.size(); ++i)    {        cout<<"name : "<<root[i]["name"].asString()<<endl;        cout<<"age : "<<root[i]["age"].asInt()<<endl;    }    return -1;}

json檔案為:

[    {"name":"qeesung1","age":21},    {"name":"qeesung2","age":22},    {"name":"qeesung3","age":23},    {"name":"qeesung4","age":24}]

編譯運行結果為:

寫入資料到SON檔案中
#include <fstream>#include <cassert>#include "json/json.h"#include <iostream>using namespace std;int main(){    ofstream outFile("test.json", ios::out | ios::trunc);    if(!outFile.is_open())    {        cerr<<"Open the file failed"<<endl;        return -1;    }    Json::Value root;    Json::FastWriter writer;    for(int k = 0 ; k < 3 ; ++k)    {        root[k]["root"]="qeesung";        root[k]["age"]=k+10;    }    string str = writer.write(root);    outFile<<str;    return -1;}

我們得到結果:

[{"age":10,"root":"qeesung"},{"age":11,"root":"qeesung"},{"age":12,"root":"qeesung"}]

C++解析JSON(jsonCpp)

聯繫我們

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