Json常用格式
2021-03-02 11:29
标签:bool dev 区别 namespace json stl roo 反序 获取 Json::Value value = "XXXXXX"; std::string str = "XXXXXX"; std::ifstream in("E:\XXXX.json"); std::ifstream in("E:\XXXX.json"); Json::Value value="XXXXXX"; 方式一: std::ifstream in("E:\XXXX.json"); std::vectorstd::string vet; nlohmann::json j; std::ofstream out("E:\XXXX.json"); std::vectorstd::string vet; Json::Value value; std::ofstream out("E:\XXXX.json"); std::vectorstd::string vet; nlohmann::json j(vet); std::vectorstd::string vet; //获取一个数组的格式 std::vectorstd::string vet; for (auto &iter : vet) { nlohmann::json rootJson = nlohmann::json::object(); //序列化diskSn //序列化smallSn和middleSn //序列化bigSn和memorySn //各层级节点挂载 //序列化 Json::Value rootValue = Json::objectValue; //序列化diskSn //序列化smallSn和middleSn //序列化bigSn和memorySn //各层级节点挂载 //序列化 std::cout
std::ifstream in("E:\XXXX.json"); try { //解析opsSn //解析diskSn //解析memorySn std::cout
for (auto &iter : diskVet) { std::ifstream in("E:\XXXX.json"); Json::Value value; //解析diskSn //解析memorySn std::cout
for (auto &iter : diskVet) { std::ifstream in("E:\XXXX.json"); std::map<:string std::string> dataMap; std::ifstream in("E:\XXXX.json"); Json::Reader reader; std::map<:string std::string> dataMap; //序列化data //层级挂载 std::cout
std::map<:string std::string> dataMap; //序列化data std::cout
Json常用格式 标签:bool dev 区别 namespace json stl roo 反序 获取 原文地址:https://www.cnblogs.com/gd-luojialin/p/14348173.html常用的json序列化工具有jsoncpp,nlohmann.(nlohmann的使用一定要捕捉异常)
解析一些常用的json格式,上述工具的使用方式分别是什么。
备注:分割线的前者是nlohmann的使用,后者是jsoncpp的使用
序列化
nlohmann::json j="XXXXXX";
std::string str = j.dump(4); //带换行缩进,参数是缩进空格数
std::string str = value.toStyledString();反序列化
std::string str = "XXXXXX";
nlohmann::json j = nlohmann::json::parse(str);
Json::Value value;
Json::Reader reader;
if (reader.parse(str, value)) {
}磁盘存取
读取
json j;
in >> j; //json数据:j
in.close();
std::string content = j.dump(4); //string数据:content
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
out.close();
std::ofstream out("E:\XXXX.json");
out
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"].getstd::string();
}
} catch (const std::exception& ex) {
}
in.close();方式二:
auto iter = j.find("deviceId");
if (iter != j.end()) {
std::string str = j["deviceId"].getstd::string();
}
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数组
vet.emplace_back("test1");
vet.emplace_back("test2");
vet.emplace_back("test3");
for (auto &iter : vet) {
j.push_back(iter);
}
out
out.close();
vet.emplace_back("test1");
vet.emplace_back("test2");
vet.emplace_back("test3");
for (unsigned i = 0; i
value[i] = vet[i];
}
out
out.close();nlohmann对于数组操作,有更为简便的优势:
vet.emplace_back("test1");
vet.emplace_back("test2");
vet.emplace_back("test3");
std::ofstream out("E:\XXXX.json");
out
out.close();反序列化Json数组到普通数组
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
}
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
std::string str = value[i].asString();
vet.emplace_back(str);
}
}
}
in.close();
std::cout
}多层级字段的序列化与反序列化
e.g.
{
"opsSn": "Default string",
"diskSn": [
"testDisk1",
"testDisk2"
],
"memorySn": {
"bigSn": "testBig",
"middleSn": {
"smallSn": "testSmall"
}
}
}
数据序列化到Json
std::vectorstd::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
std::vectorstd::string vet;
vet.emplace_back("test1");
vet.emplace_back("test2");
Json::Value vetValue = Json::arrayValue;
for (auto &iter : vet) {
vetValue.append(iter);
}
//或者
//for (unsigned i = 0; i
// //vetValue[i] = vet[i];
//}
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();备注:nlohmann::json在STL容器兼容方面很简便。无论是vector,set,map,deque,list等
反序列化Json到数据
std::string opsSn;
std::vectorstd::string diskVet;
std::string bigSn;
std::string smallSn;
nlohmann::json j = nlohmann::json::parse(in);
in.close();
if (j.contains("opsSn") && j["opsSn"].is_string()) {
opsSn = j.at("opsSn").getstd::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").getstd::string();
}
//解析middleSn
if (memJson["middleSn"].is_object()) {
nlohmann::json middleJson = memJson["middleSn"];
if (middleJson.contains("smallSn") && middleJson["smallSn"].is_string()) {
smallSn = middleJson.at("smallSn").getstd::string();
}
}
}
} catch (const std::exception&) {
}
std::cout
}
std::cout
std::cout
std::string opsSn;
std::vectorstd::string diskVet;
std::string bigSn;
std::string smallSn;
Json::Reader reader;
if (reader.parse(in, value)) {
//解析opsSn
if (value.isMember("opsSn") && value["opsSn"].isString()) {
opsSn = value["opsSn"].asString();
}
if (value["diskSn"].isArray()) {
for (unsigned i = 0; 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();
}
//解析middleSn
if (memValue["middleSn"].isObject()) {
Json::Value midSnValue = memValue["middleSn"];
if (midSnValue.isMember("smallSn") && midSnValue["smallSn"].isString()) {
smallSn = midSnValue["smallSn"].asString();
}
}
}
}
in.close();
std::cout
}
std::cout
std::cout
备注:在解析json,两者没有明显的区别与优势
Json对象里包含多个键值对(可能不知道主键,也拿到对应的值)
e.g.
{
"data":{
"test1":"this is test1",
"test2":"this is test2",
"test3":"this is test3",
"test4":"this is test4"
}
}
反序列化Json到数据
json j;
in >> j;
in.close();
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<:string std::string>(key, val));
}
}
} catch (const std::exception&) {
}
for (auto &iter : dataMap) {
std::cout
}
std::map<:string std::string> dataMap;
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<:string std::string>(key, val));
}
}
}
in.close();
for (auto &iter : dataMap) {
std::cout
}数据序列化到Json
dataMap.insert(std::pair<:string std::string>("test1", "this is test1"));
dataMap.insert(std::pair<:string std::string>("test2", "this is test2"));
dataMap.insert(std::pair<:string std::string>("test3", "this is test3"));
dataMap.insert(std::pair<:string std::string>("test4", "this is test4"));
nlohmann::json j(dataMap);
nlohmann::json rootJ = nlohmann::json::object();
rootJ["data"] = j;
dataMap.insert(std::pair<:string std::string>("test1", "this is test1"));
dataMap.insert(std::pair<:string std::string>("test2", "this is test2"));
dataMap.insert(std::pair<:string std::string>("test3", "this is test3"));
dataMap.insert(std::pair<: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;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);
}
} // namespace ns
优势:不需要知道person中成员的数据类型