今天复习一下jsoncpp解析库的用法
Github传送门
下载工程源码:
#pragma once
#include "json.h"
#include
using namespace std;struct Score
{string subject;float score;bool bChecked;Score(const string& nm, float fS,bool bV = true):subject(nm),score(fS),bChecked(bV){bChecked = true;}
};
struct Student
{string name;vector arr;
};class JsoncppAssistant
{
private:JsoncppAssistant() = default;
public:~JsoncppAssistant() = default;JsoncppAssistant(const JsoncppAssistant& src) = delete;JsoncppAssistant& operator=(const JsoncppAssistant& src) = delete;
public:bool Read(vector& arr, string path = "");bool Write(const vector& arr);
public:static JsoncppAssistant* GetInstance(){static JsoncppAssistant ins;return &ins;}
};extern JsoncppAssistant* GetJscppMgr();
#include "JsoncppAssistant.h"
#include
#include JsoncppAssistant* GetJscppMgr()
{return JsoncppAssistant::GetInstance();
}bool JsoncppAssistant::Read(vector& arr, string path)
{arr.clear();Json::Reader reader;Json::Value root;ifstream ifs;ifs.open("test.json");if (!ifs.is_open()){return false;}if (!reader.parse(ifs, root)){return false;}if (root["data"].isNull()) return false;int nSize = root["data"].size();for (int i = 0; i < nSize; i++){Student stu;stu.name = root["data"][i]["name"].asString();if (!root["data"][i]["Score"].isNull()){int nSz2 = root["data"][i]["Score"].size();stu.arr.reserve(nSz2);for (int j = 0; j < nSz2; j++){stu.arr.push_back(Score(root["data"][i]["Score"][j]["subject"].asString(),root["data"][i]["Score"][j]["score"].asFloat(),root["data"][i]["Score"][j]["bChecked"].asBool()));}}arr.push_back(stu);}return true;
}bool JsoncppAssistant::Write(const vector&arr)
{if (arr.empty()) return false;Json::Value root;Json::StyledWriter writer;for (auto& it : arr){Json::Value JsStu;JsStu["name"] = it.name;for (auto& it2 : it.arr){Json::Value JsScore;JsScore["subject"] = it2.subject;JsScore["score"] = it2.score;JsScore["bChecked"] = it2.bChecked;JsStu["Score"].append(JsScore);}root["data"].append(JsStu);}string json_file = writer.write(root);ofstream ofs;ofs.open("test.json");if (!ofs.is_open()){return false;}ofs << json_file;ofs.close();return true;
}
void Write()
{vector arr;Student aaa;aaa.name = "aaa";aaa.arr.push_back(Score("c", 60.5));aaa.arr.push_back(Score("cpp", 88.5));aaa.arr.push_back(Score("python", 99.5));arr.push_back(aaa);Student bbb;bbb.name = "bbb";bbb.arr.push_back(Score("c", 77.5));bbb.arr.push_back(Score("cpp", 67));bbb.arr.push_back(Score("python", 95));arr.push_back(bbb);Student ccc;ccc.name = "ccc";ccc.arr.push_back(Score("c", 85));ccc.arr.push_back(Score("cpp", 76.5));ccc.arr.push_back(Score("python", 69.5));arr.push_back(ccc);GetJscppMgr()->Write(arr);
}
bool JsoncppAssistant::Read(vector& arr, string path)
{arr.clear();Json::Reader reader;Json::Value root;ifstream ifs;ifs.open("test.json");if (!ifs.is_open()){return false;}if (!reader.parse(ifs, root)){return false;}if (root["data"].isNull()) return false;int nSize = root["data"].size();for (int i = 0; i < nSize; i++){Student stu;stu.name = root["data"][i]["name"].asString();if (!root["data"][i]["Score"].isNull()){int nSz2 = root["data"][i]["Score"].size();stu.arr.reserve(nSz2);for (int j = 0; j < nSz2; j++){stu.arr.push_back(Score(root["data"][i]["Score"][j]["subject"].asString(),root["data"][i]["Score"][j]["score"].asFloat(),root["data"][i]["Score"][j]["bChecked"].asBool()));}}arr.push_back(stu);}return true;
}