標籤: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.cpp,json_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)