#include <iostream>
#include <fstream>
#include <string>
#include <rapidjson/document.h>
#include <boost/filesystem.hpp>
using namespace std;
using namespace rapidjson;
int main(int argc, const char *argv[]) {
string filename = "test.json";
if (boost::filesystem::exists(filename)) {
std::cout << "File exists!" << std::endl;
std::ifstream ifs(filename.c_str());
std::string content((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
Document document;
document.Parse(content.c_str());
// int_key.
if (document.HasMember("int_key")) {
const Value& int_key = document["int_key"];
if (int_key.IsInt()) {
cout << "int_key: " << int_key.GetInt() << endl;
}
}
cout << "---------------------------------------------------------------" << endl;
// str_key.
if (document.HasMember("str_key")) {
const Value& str_key = document["str_key"];
if (str_key.IsString()) {
cout << "str_key: " << str_key.GetString() <<endl;
}
}
cout << "---------------------------------------------------------------" << endl;
// int_list_key.
if (document.HasMember("int_list_key")) {
const Value& int_list_key = document["int_list_key"];
if (int_list_key.IsArray()) {
for (Value::ConstValueIterator itr = int_list_key.Begin(); itr != int_list_key.End(); ++itr) {
std::cout << itr->GetInt() << std::endl;
}
}
}
cout << "---------------------------------------------------------------" << endl;
// str_list_key.
if (document.HasMember("str_list_key")) {
const Value& str_list_key = document["str_list_key"];
if (str_list_key.IsArray()) {
for (Value::ConstValueIterator itr = str_list_key.Begin(); itr != str_list_key.End(); ++itr) {
std::cout << itr->GetString() << std::endl;
}
}
}
cout << "---------------------------------------------------------------" << endl;
// bool_key.
if (document.HasMember("bool_key")) {
const Value& bool_key = document["bool_key"];
if (bool_key.IsBool()) {
cout << "bool_key: " << bool_key.GetBool() <<endl;
if (bool_key.IsFalse()) {
cout << "bool_key.IsFalse()" <<endl;
} else if (bool_key.IsTrue()) {
cout << "bool_key.IsTrue()" <<endl;
}
}
}
cout << "---------------------------------------------------------------" << endl;
// null_key.
if (document.HasMember("null_key")) {
const Value& null_key = document["null_key"];
if (null_key.IsNull()) {
cout << "null_key.IsNull()" <<endl;
}
}
cout << "---------------------------------------------------------------" << endl;
// double_key.
if (document.HasMember("double_key")) {
const Value& double_key = document["double_key"];
if (double_key.IsDouble()) {
// cout << "double_key: " << double_key.GetDouble() <<endl;
double value = double_key.GetDouble();
printf("double_key: %.16g\n", value); // 这种方法不会损失精度
}
}
cout << "---------------------------------------------------------------" << endl;
// data_key
if (document.HasMember("data_key")) {
const Value& data_key = document["data_key"];
if (data_key.IsObject()) {
cout << "data_key yes" << endl;
}
}
} else {
cout << "File does not exist!" << endl;
}
return 0;
}
// test.json
{
"int_key": 10,
"str_key": "OnePiece",
"int_list_key": [1, 2, 3, 4, 5],
"str_list_key": ["1_str", "2_str", "3_str", "4_str", "5_str"],
"bool_key": false,
"null_key": null,
"double_key": 3.1415926,
"data_key": {
"sub_key": "DataCore"
}
}