https://en.wikibooks.org/wiki/JsonCpp
http://blog.csdn.net/yc461515457/article/details/52749575
Jsoncpp是一个使用C++语言实现的面向对象的json库。
Jsoncpp提供的接口中有3个核心类:Reader、Writer、Value。
Reader类负责从字符串或者输入流中加载JSON文档,并进行解析,生成代表JSON文档的Value对象。
Writer类负责将内存中的Value对象转换成JSON文档,可输出到文件或者是字符串中。
Value类的对象代表一个JSON值,既可以代表一个文档,也可以代表文档中一个值。
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