(2條消息) nlohmann json使用_nlohmann::json_蝸牛單行道的博客-CSDN博客json為JavaScript object notation 是一種數(shù)據(jù)格式,逐漸替換掉了傳統(tǒng)的xml 。json數(shù)據(jù)格式的屬性名稱和字符串值需要用雙引號引起來,用單引號或者不用引號會導致讀取數(shù)據(jù)錯誤。json的另外一個數(shù)據(jù)格式是數(shù)組,和javascript中的數(shù)組字面量相同。
使用Json的格式與解析方便的可以表示一個對象信息,json有兩種格式:
json中不能有注釋,undefined,只要涉及到字符串的就必須雙引號
①對象格式:{"key1":obj,"key2":obj,"key3":obj...}、
②數(shù)組/集合格式:[obj,obj,obj...]。
json.parse()解析將字符串解析成對應的值,
例子:
將nlohmann/json: JSON for Modern C++ (github.com)
?中代碼下載下來,然后將include文件夾添加到測試工程的包含路徑下即可:
我是用的絕對路徑:D:\excer\mfc\json\json\include\;?
?測試代碼:
#include <iostream>
#include <fstream>
#include <ostream>
#include "json.hpp"
using json = nlohmann::json;
int main()
{
std::cout << "Hello World!\n";
//讀取json文件--------------------------------
std::ifstream f("example.json");
json data = json::parse(f);
double pi = data["pi"];
bool td = false;
//td = data["happy"];
if (!data["happy"].is_null())
{
td = data["happy"];
}
std::string sname = data["name"].get<std::string>(); ;
std::cout << data;
int c = 0;
//輸出json文件--------------------------------
// create an empty structure (null)
json j;
//方式一---------------------------------------
// add a number that is stored as double (note the implicit conversion of j to an object)
j["pi"] = 3.141;
// add a Boolean that is stored as bool
j["happy"] = true;
// add a string that is stored as std::string
j["name"] = "Niels";
// add another null object by passing nullptr
j["nothing"] = nullptr;
// add an object inside the object
j["answer"]["everything"] = 42;
// add an array that is stored as std::vector (using an initializer list)
j["list"] = { 1, 0, 2 };
// add another object (using an initializer list of pairs)
j["object"] = { {"currency", "USD"}, {"value", 42.99} };
//方式二---------------------------------------
// instead, you could also write (which looks very similar to the JSON above)
json j2 = {
{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
{"nothing", nullptr},
{"answer", {
{"everything", 42}
}},
{"list", {1, 0, 2}},
{"object", {
{"currency", "USD"},
{"value", 42.99}
}}
};
//輸出到文件中
std::ofstream fout("out.json");
fout << std::setw(4)<< j;
fout << std::setw(4) << j2;
fout.close();
}
對應的:json格式
{
"pi": 3.141,
"happy": true,
"name": "Niels",
"nothing": null,
"answer": {
"everything": 42
},
"list": [1, 0, 2],
"object": {
"currency": "USD",
"value": 42.99
}
}
?異常處理:
標簽(關(guān)鍵字)happy2不存在,只有標簽(關(guān)鍵字)happy,如果執(zhí)行該語句bool td = data["happy2"]; 在直接讀取標簽happy2則解析器會崩潰。
解決辦法:下面先對標簽判斷一下是否為空,為空的話,就不要直接讀取了。 標簽不為空才能直接讀取
//td = data["happy"];
if (!data["happy"].is_null())//關(guān)鍵字不為空才能直接讀取
{
td = data["happy"];
}
或者用contain
if (js.contains("exposure"))
{
// expos = js["exposure"];//不建議
//.get<std::string>()
expos = js["exposure"].get<int>();//建議顯示類型轉(zhuǎn)換
strTemp.Format(L"%d", expos);
SetDlgItemText(IDC1_EDIT1, strTemp);;
}
?
?異常2:修改json文件中個別關(guān)鍵字時
按照直接修改js["exposure"] = expos;寫到j(luò)son文件時,文件會出現(xiàn)重復的關(guān)鍵字,后面json再讀取解析文件時就會崩潰,解決辦法為: 先讀json文件,然后解析,解析后賦值給中間變量,
修改中間變量中的關(guān)鍵字,然后再保存json文件就可以了。
std::ifstream ifs("config.json", std::ios::app);//先讀json文件
json ss = json::parse(ifs);//解析json文件
json js = ss;//中間變量js
//js = ss;
CString strTemp;
GetDlgItemText(IDC1_EDIT1, strTemp);
int expos = _ttoi(strTemp);
if (js.contains("exposure"))
{
js["exposure"] = expos;//修改中間變量js中關(guān)鍵字exposure
}
std::ofstream fs("config.json");//最后寫json文件
fs << std::setw(4)<< js << std::endl;;//setw(4) 是用于打印格式好看的 json 文件//使用 j.dump(4) 也是一樣的效果
//fs << js;
fs.close();
參考:
Issues · nlohmann/json (github.com)https://github.com/nlohmann/json/issues/1475
主要看下面的文章:
寫json格式
c++中nlohmann?json的基本使用教程_C 語言_腳本之家 (jb51.net)
nlohmann/json: JSON for Modern C++ (github.com)https://github.com/nlohmann/json/tree/develop
JSON的三種格式https://blog.csdn.net/daxiong0816/article/details/125132404
json格式 (keoaeic.org)https://mip.keoaeic.org/unscramble_major/4394.htmlC++ json格式的書寫_雪星途的博客-CSDN博客https://blog.csdn.net/weixin_45387966/article/details/122469835文章來源:http://www.zghlxwxcb.cn/news/detail-481055.html
(2條消息) nlohmann json使用_nlohmann::json_蝸牛單行道的博客-CSDN博客https://blog.csdn.net/qq_39568245/article/details/115312690?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-0-115312690-blog-121181848.235%5Ev38%5Epc_relevant_anti_vip_base&spm=1001.2101.3001.4242.1&utm_relevant_index=3文章來源地址http://www.zghlxwxcb.cn/news/detail-481055.html
到了這里,關(guān)于c++ nlohmann/json 及修改json文件中個別關(guān)鍵字 JSON的三種格式https://blog.csdn.net/daxiong0816/article/details/125132404的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!