使用Json模块开源项目,Github地址:
https://github.com/nlohmann/json
直接将json.hpp 放入到文件夹中即可。
#include "json.hpp"
#include <fstream>
#include <iostream>
using namespace std;
using json = nlohmann::json;
int main()
{
// write to file
/*
json outputjson;
outputjson["occupation"] = "paladin";
outputjson["camp"] = "alliance";
outputjson["role_id"] = 1;
ofstream os;
os.open("****.json");
os << outputjson;
os.close();
*/
// read json file
std::ifstream input("******.json");
json inputjson;
input >> inputjson;
std::string strNameAAA = inputjson["aaa"];
std::string strNameBBB = inputjson["bbb"];
int intNameCCC = inputjson["ccc"];
std::cout << "AAA: " << strNameAAA << std::endl;
std::cout << "BBB: " << strNameBBB << std::endl;
std::cout << "CCC: " << intNameCCC << std::endl;
return 0;
}