https://en.wikibooks.org/wiki/JsonCpp
http://blog.csdn.net/yc461515457/article/details/52749575
Jsoncpp是一個(gè)使用C++語(yǔ)言實(shí)現(xiàn)的面向?qū)ο蟮膉son庫(kù)玷或。
Jsoncpp提供的接口中有3個(gè)核心類:Reader喳篇、Writer腿箩、Value。
Reader類負(fù)責(zé)從字符串或者輸入流中加載JSON文檔,并進(jìn)行解析,生成代表JSON文檔的Value對(duì)象。
Writer類負(fù)責(zé)將內(nèi)存中的Value對(duì)象轉(zhuǎn)換成JSON文檔,可輸出到文件或者是字符串中伦籍。
Value類的對(duì)象代表一個(gè)JSON值,既可以代表一個(gè)文檔腮出,也可以代表文檔中一個(gè)值帖鸦。
Install JsonCpp.
Create file alice.json with the following contents:
{
"book":"Alice in Wonderland",
"year":1865,
"characters":
[
{"name":"Jabberwock", "chapter":1},
{"name":"Cheshire Cat", "chapter":6},
{"name":"Mad Hatter", "chapter":7}
]
}
Create file alice.cpp with following contents:
include <fstream>
include <jsoncpp/json/json.h> // or jsoncpp/json.h , or json/json.h etc.
using namespace std;
int main() {
ifstream ifs("alice.json");
Json::Reader reader;
Json::Value obj;
reader.parse(ifs, obj); // reader can also read strings
cout << "Book: " << obj["book"].asString() << endl;
cout << "Year: " << obj["year"].asUInt() << endl;
const Json::Value& characters = obj["characters"]; // array of characters
for (int i = 0; i < characters.size(); i++){
cout << " name: " << characters[i]["name"].asString();
cout << " chapter: " << characters[i]["chapter"].asUInt();
cout << endl;
}
}
Compile it:
g++ -o alice alice.cpp -ljsoncpp
Then run it:
./alice
You will hopefully receive the following:
Book: Alice in Wonderland
Year: 1865
name: Jabberwock chapter: 1
name: Cheshire Cat chapter: 6
name: Mad Hatter chapter: 7