常用的json序列化工具有jsoncpp,nlohmann.(nlohmann的使用一定要捕捉异常)
解析一些常用的json格式,上述工具的使用方式分别是什么。
备注:分割线的前者是nlohmann的使用,后者是jsoncpp的使用
序列化
nlohmann::json j="XXXXXX";
std::string str = j.dump(4);
Json::Value value = "XXXXXX";
std::string str = value.toStyledString();
反序列化
std::string str = "XXXXXX";
nlohmann::json j = nlohmann::json::parse(str);
std::string str = "XXXXXX";
Json::Value value;
Json::Reader reader;
if (reader.parse(str, value)) {
}
磁盘存取
读取
std::ifstream in("E:\\XXXX.json");
json j;
in >> j; //json数据:j
in.close();
std::string content = j.dump(4); //string数据:content
std::ifstream in("E:\\XXXX.json");
Json::Value value;
Json::Reader reader;
if (reader.parse(in, value)) {
//json数据:value ;string数据:content
std::string content = value.toStyledString();
}
in.close();
写入
nlohmann::json j="XXXXXX"
std::ofstream out("E:\\XXXX.json")
out<<j.dump(4)
out.close()
Json::Value value="XXXXXX"
std::ofstream out("E:\\XXXX.json")
out<<value.toStyledString()
out.close()
判断字段是否存在,并且获取对应的字段
类型主要有:null,bool,object,array,string,number.
方式一:
std::ifstream in("E:\\XXXX.json");
try {
nlohmann::json j = nlohmann::json::parse(in);
if (j.contains("password") && j["password"].is_string()) {
std::string str = j["password"].get<std::string>();
}
} catch (const std::exception& ex) {
}
in.close();
方式二:
auto iter = j.find("deviceId");
if (iter != j.end()) {
std::string str = j["deviceId"].get<std::string>();
}
std::ifstream in("E:\\XXXX.json");
Json::Value value;
Json::Reader reader;
if (reader.parse(in, value)) {
if (value.isMember("password") && value["password"].isString()) {
std::string str = value["password"].asString();
}
}
in.close();
数组的序列化与反序列化
e.g.
[
"test1",
"test2",
"test3"
]
普通数组序列化到Json数组
std::vector<std::string> vet;
vet.emplace_back("test1");
vet.emplace_back("test2");
vet.emplace_back("test3");
nlohmann::json j;
for (auto &iter : vet) {
j.push_back(iter);
}
std::ofstream out("E:\\XXXX.json");
out << j.dump(4);
out.close();
std::vector<std::string> vet
vet.emplace_back("test1")
vet.emplace_back("test2")
vet.emplace_back("test3")
Json::Value value
for (unsigned i = 0
value[i] = vet[i]
}
std::ofstream out("E:\\XXXX.json")
out << value.toStyledString()
out.close()
nlohmann对于数组操作,有更为简便的优势:
std::vector<std::string> vet
vet.emplace_back("test1")
vet.emplace_back("test2")
vet.emplace_back("test3")
nlohmann::json j(vet)
std::ofstream out("E:\\XXXX.json")
out << j.dump(4)
out.close()
反序列化Json数组到普通数组
std::vector<std::string> vet;
std::ifstream in("E:\\XXXX.json");
json j;
in >> j;
in.close();
if (j.is_array()) {
for (auto iter = j.begin(); iter != j.end(); ++iter) {
std::string str = *iter;
vet.emplace_back(str);
}
}
for (auto &iter : vet) {
std::cout << iter;
}
std::vector<std::string> vet;
std::ifstream in("E:\\XXXX.json");
Json::Value value;
Json::Reader reader;
if (reader.parse(in, value)) {
if (value.isArray()) {
for (unsigned i = 0; i < value.size(); ++i) {
std::string str = value[i].asString();
vet.emplace_back(str);
}
}
}
in.close();
for (auto &iter : vet) {
std::cout << iter;
}
多层级字段的序列化与反序列化
e.g.
{
"opsSn": "Default string",
"diskSn": [
"testDisk1",
"testDisk2"
],
"memorySn": {
"bigSn": "testBig",
"middleSn": {
"smallSn": "testSmall"
}
}
}
数据序列化到Json
nlohmann::json rootJson = nlohmann::json::object();
std::vector<std::string> vet;
vet.emplace_back("test1");
vet.emplace_back("test2");
nlohmann::json vetJson(vet);
nlohmann::json middleJson = nlohmann::json::object();
middleJson["smallSn"] = "testSmall";
nlohmann::json memJson = nlohmann::json::object();
memJson["bigSn"] = "testBig";
memJson["middleSn"] = middleJson;
rootJson["opsSn"] = "Default string";
rootJson["diskSn"] = vetJson;
rootJson["memorySn"] = memJson;
std::string str = rootJson.dump(4);
std::cout << str;
Json::Value rootValue = Json::objectValue;
std::vector<std::string> vet;
vet.emplace_back("test1");
vet.emplace_back("test2");
Json::Value vetValue = Json::arrayValue;
for (auto &iter : vet) {
vetValue.append(iter);
}
Json::Value middleValue = Json::objectValue;
middleValue["smallSn"] = "testSmall";
Json::Value memValue = Json::objectValue;
memValue["bigSn"] = "testBig";
rootValue["opsSn"] = "Default string";
memValue["middleSn"] = middleValue;
rootValue["diskSn"] = vetValue;
rootValue["memorySn"] = memValue;
std::string str = rootValue.toStyledString();
std::cout << str;
备注:nlohmann::json在STL容器兼容方面很简便。无论是vector,set,map,deque,list等
反序列化Json到数据
std::ifstream in("E:\\XXXX.json");
std::string opsSn;
std::vector<std::string> diskVet;
std::string bigSn;
std::string smallSn;
try {
nlohmann::json j = nlohmann::json::parse(in);
in.close();
if (j.contains("opsSn") && j["opsSn"].is_string()) {
opsSn = j.at("opsSn").get<std::string>();
}
if (j["diskSn"].is_array()) {
for (auto &iter : j["diskSn"]) {
std::string str = iter;
diskVet.emplace_back(str);
}
}
if (j["memorySn"].is_object()) {
nlohmann::json memJson = j["memorySn"];
if (memJson.contains("bigSn") && memJson["bigSn"].is_string()) {
bigSn = memJson.at("bigSn").get<std::string>();
}
if (memJson["middleSn"].is_object()) {
nlohmann::json middleJson = memJson["middleSn"];
if (middleJson.contains("smallSn") && middleJson["smallSn"].is_string()) {
smallSn = middleJson.at("smallSn").get<std::string>();
}
}
}
} catch (const std::exception&) {
}
std::cout << opsSn << std::endl;
for (auto &iter : diskVet) {
std::cout << iter << std::endl;
}
std::cout << bigSn << std::endl;
std::cout << smallSn << std::endl;
std::ifstream in("E:\\XXXX.json");
std::string opsSn;
std::vector<std::string> diskVet;
std::string bigSn;
std::string smallSn;
Json::Value value;
Json::Reader reader;
if (reader.parse(in, value)) {
if (value.isMember("opsSn") && value["opsSn"].isString()) {
opsSn = value["opsSn"].asString();
}
if (value["diskSn"].isArray()) {
for (unsigned i = 0; i < value["diskSn"].size(); ++i) {
std::string str = value["diskSn"][i].asString();
diskVet.emplace_back(str);
}
}
if (value["memorySn"].isObject()) {
Json::Value memValue = value["memorySn"];
if (memValue.isMember("bigSn") && memValue["bigSn"].isString()) {
bigSn = memValue["bigSn"].asString();
}
if (memValue["middleSn"].isObject()) {
Json::Value midSnValue = memValue["middleSn"];
if (midSnValue.isMember("smallSn") && midSnValue["smallSn"].isString()) {
smallSn = midSnValue["smallSn"].asString();
}
}
}
}
in.close();
std::cout << opsSn << std::endl;
for (auto &iter : diskVet) {
std::cout << iter << std::endl;
}
std::cout << bigSn << std::endl;
std::cout << smallSn << std::endl;
备注:在解析json,两者没有明显的区别与优势
Json对象里包含多个键值对(可能不知道主键,也拿到对应的值)
e.g.
{
"data":{
"test1":"this is test1",
"test2":"this is test2",
"test3":"this is test3",
"test4":"this is test4"
}
}
反序列化Json到数据
std::ifstream in("E:\\XXXX.json");
json j;
in >> j;
in.close();
std::map<std::string, std::string> dataMap;
try {
if (j["data"].is_object()) {
nlohmann::json dataJson = j["data"];
for (json::iterator it = dataJson.begin(); it != dataJson.end(); ++it) {
std::string key = it.key();
std::string val = it.value();
dataMap.insert(std::pair<std::string, std::string>(key, val));
}
}
} catch (const std::exception&) {
}
for (auto &iter : dataMap) {
std::cout << iter.first << " " << iter.second << std::endl;
}
std::ifstream in("E:\\XXXX.json");
std::map<std::string, std::string> dataMap;
Json::Reader reader;
Json::Value value;
if (reader.parse(in, value)) {
if (value["data"].isObject()) {
Json::Value dataValue = value["data"];
Json::Value::Members members = dataValue.getMemberNames();
for (Json::Value::Members::iterator it = members.begin(); it != members.end(); ++it) {
std::string key = *it;
std::string val = dataValue[*it].asString();
dataMap.insert(std::pair<std::string, std::string>(key, val));
}
}
}
in.close();
for (auto &iter : dataMap) {
std::cout << iter.first << " " << iter.second << std::endl;
}
数据序列化到Json
std::map<std::string, std::string> dataMap;
dataMap.insert(std::pair<std::string, std::string>("test1", "this is test1"));
dataMap.insert(std::pair<std::string, std::string>("test2", "this is test2"));
dataMap.insert(std::pair<std::string, std::string>("test3", "this is test3"));
dataMap.insert(std::pair<std::string, std::string>("test4", "this is test4"));
nlohmann::json j(dataMap);
nlohmann::json rootJ = nlohmann::json::object();
rootJ["data"] = j;
std::cout << rootJ.dump(4);
std::map<std::string, std::string> dataMap;
dataMap.insert(std::pair<std::string, std::string>("test1", "this is test1"));
dataMap.insert(std::pair<std::string, std::string>("test2", "this is test2"));
dataMap.insert(std::pair<std::string, std::string>("test3", "this is test3"));
dataMap.insert(std::pair<std::string, std::string>("test4", "this is test4"));
Json::Value dataValue = Json::objectValue;
for (auto &iter : dataMap) {
dataValue[iter.first] = iter.second;
}
Json::Value rootValue = Json::objectValue;
rootValue["data"] = dataValue;
std::cout << rootValue.toStyledString();
Json与结构体的封装
namespace ns {
void to_json(json& j, const person& p) {
j = json{ { "name", p.name },{ "address", p.address },{ "age", p.age } };
}
void from_json(const json& j, person& p) {
j.at("name").get_to(p.name);
j.at("address").get_to(p.address);
j.at("age").get_to(p.age);
}
}
优势:不需要知道person中成员的数据类型
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 本地部署 DeepSeek:小白也能轻松搞定!
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 从 Windows Forms 到微服务的经验教训
· 李飞飞的50美金比肩DeepSeek把CEO忽悠瘸了,倒霉的却是程序员
· 超详细,DeepSeek 接入PyCharm实现AI编程!(支持本地部署DeepSeek及官方Dee