json for modern c++,这是我用过的最好用的json库了
GitHub開(kāi)源項(xiàng)的地址:https://github.com/nlohmann/json
json for modern c++是一個(gè)德國(guó)大牛nlohmann寫(xiě)的,有以下特點(diǎn):
1.直觀的語(yǔ)法。
2.整個(gè)代碼由一個(gè)頭文件組成json.hpp,沒(méi)有子項(xiàng)目,沒(méi)有依賴關(guān)系,沒(méi)有復(fù)雜的構(gòu)建系統(tǒng),使用起來(lái)非常方便。
3.使用c++11標(biāo)準(zhǔn)編寫(xiě)。
4.使用json 像使用STL容器一樣。
5.STL和json容器之間可以相互轉(zhuǎn)換。
還有很多其他的特性,不一一列舉了。
如何使用?
將github上的src文件夾里的json.hpp頭文件下載保存到當(dāng)前目錄中。
在代碼中包含json.hpp頭文件并引入json作用域
?
常用功能:
一.創(chuàng)建json對(duì)象
1.使用cin,cout輸入輸出流。
json提供了cin,cout的輸入輸出流的操作符。但需要注意的是,cin要有ctr + D結(jié)束輸入。cin會(huì)把從標(biāo)準(zhǔn)輸入的內(nèi)容反序列化,cout會(huì)自動(dòng)把json序列化,以string形式輸出。
2.提供根據(jù)鍵直接生成鍵值對(duì)的方法。(類似于map,如果不存在該鍵的話,就生成一個(gè)這個(gè)鍵。)
#include <iostream> #include "json.hpp" using json = nlohmann::json; using namespace std;int main() {// 創(chuàng)建一個(gè)json對(duì)象(null)json j;//添加一個(gè)存儲(chǔ)為double的數(shù)字j["pi"] = 3.141;// 添加一個(gè)布爾值 j["happy"] = true;// 添加一個(gè)存儲(chǔ)為std :: string的字符串j["name"] = "Niels";// 通過(guò)傳遞nullptr添加另一個(gè)空對(duì)象j["nothing"] = nullptr;// 在對(duì)象中添加對(duì)象j["answer"]["everything"] = 42;//添加一個(gè)數(shù)組,其存儲(chǔ)為std::vector(使用初始化列表)j["list"] = { 1, 0, 2 };// 在一個(gè)對(duì)象中添加另一個(gè)對(duì)象j["object"] = { {"currency", "USD"}, {"value", 42.99} };// 也可以通過(guò)直接賦值方式創(chuàng)建json對(duì)象,兩種方式創(chuàng)建結(jié)果相同json j2 = {{"pi", 3.141},{"happy", true},{"name", "Niels"},{"nothing", nullptr},{"answer", {{"everything", 42}}},{"list", {1, 0, 2}},{"object", {{"currency", "USD"},{"value", 42.99}}}};cout << j << endl;cout << endl;cout << j2 << endl;return 0; }?
3.在所有上述情況下,你不需要“告訴”編譯器要使用哪個(gè)JSON值。如果你想明確或表達(dá)一些邊緣的情況下,可以使用json::array,json::object。
#include <iostream> #include "json.hpp" using json = nlohmann::json; using namespace std;int main() {//創(chuàng)建一個(gè)空數(shù)組json empty_array_explicit = json::array();// 創(chuàng)建一個(gè)空對(duì)象的兩種方式j(luò)son empty_object_implicit = json({});json empty_object_explicit = json::object(); }?
4.幾點(diǎn)注意
1).array是一個(gè)數(shù)組,可以用數(shù)字直接下標(biāo)訪問(wèn)。
?
2)數(shù)組中包含一個(gè)數(shù)組
json array = {{"a",6},"xin",8}; cout?<<?array[0][0]?<<?endl;??//輸出數(shù)組里的數(shù)組元素中的第一個(gè)元素的值?
3)數(shù)組中包含一個(gè)對(duì)象
json array = {{{"a",6}},"xin",8}; //輸出數(shù)組中第一個(gè)元素"a"對(duì)應(yīng)的鍵值 cout?<<?array[0]["a"]?<<?endl;?
4).對(duì)象中嵌套一個(gè)對(duì)象和一個(gè)數(shù)組
json object = {{"a",{{"feng",6}}},{"b",{1,2,3}}};cout << object << endl; /*輸出: {"a":{"feng":6},"b":[1,2,3]} */?
二.序列化
將json對(duì)象序列化,成為字符串
1.標(biāo)準(zhǔn)輸出自動(dòng)序列化
json j std :: cout << j; // setw操縱器被重載以設(shè)置漂亮打印的縮進(jìn) std :: cout << std :: setw( 4)<< j << std :: endl;?
2.使用dump()函數(shù)
//顯式轉(zhuǎn)換為string std::string s = j.dump(); // {\"happy\":true,\"pi\":3.141}//序列化與漂亮的打印 //傳入空格的數(shù)量縮進(jìn) std::cout << j.dump(4) << std::endl; // 輸出: //{ // "happy": true, // "pi": 3.141 // }?
三.反序列化
將數(shù)據(jù)流轉(zhuǎn)化為json對(duì)象
1.從標(biāo)準(zhǔn)輸入反序列化
json j std :: cin >> j;?
2.通過(guò)附加_json到字符串文字來(lái)創(chuàng)建對(duì)象(反序列化):
//從字符串文字創(chuàng)建對(duì)象 json j = " { \" happy \":true,\" pi \":3.141} " _json;//或者原始字符串文字 auto j2 = R"({ "happy":true,"pi":3.141 } )"?_json;?
請(qǐng)注意,沒(méi)有附加_json后綴,傳遞的字符串文字不會(huì)被解析,而只是用作JSON字符串值。也就是說(shuō),json j = "{ \"happy\": true, \"pi\": 3.141 }"只存儲(chǔ)字符串"{ "happy": true, "pi": 3.141 }"而不是解析實(shí)際的對(duì)象。
3.使用json::parse()函數(shù)
?
4.從迭代器范圍讀取
您還可以從迭代器范圍讀取JSON; 也就是說(shuō),可以從其內(nèi)容存儲(chǔ)為連續(xù)字節(jié)序列的迭代器訪問(wèn)的任何容器,例如std::vector
std :: vector < uint8_t > v = { ' t ',' r ',' u ',' e ' }; json j = json :: parse(v.begin(),v.end());//或 std :: vector < uint8_t > v = { ' t ',' r ',' u ',' e ' }; json?j?=?json?::?parse(v);?
四.與STL適應(yīng)
nlohmann設(shè)計(jì)的JSON類,就像一個(gè)STL容器一樣。其實(shí)它滿足了可逆容器要求。
//使用push_back創(chuàng)建一個(gè)數(shù)組 json j; j.push_back("foo"); j.push_back(1); j.push_back(true);// 也可以使用emplace_back j.emplace_back(1.78);// 使用迭代器訪問(wèn) for (json::iterator it = j.begin(); it != j.end(); ++it) {std::cout << *it << '\n'; }// 基于范圍for循環(huán) for (auto& element : j) {std::cout << element << '\n'; }// 賦值操作 const std::string tmp = j[0]; j[1] = 42; bool foo = j.at(2);// 比較操作 j == "[\"foo\", 1, true]"_json; // true// 其他函數(shù) j.size(); // 3 j.empty(); // false j.type(); // json::value_t::array j.clear(); // 使數(shù)組再次為空// 方便類型檢查 j.is_null(); j.is_boolean(); j.is_number(); j.is_object(); j.is_array(); j.is_string();// 創(chuàng)建一個(gè)數(shù)組 json o; o["foo"] = 23; o["bar"] = false; o["baz"] = 3.141;// 也可以使用emplace o.emplace("weather", "sunny");// 使用迭代器輸出 for (json::iterator it = o.begin(); it != o.end(); ++it) {std::cout << it.key() << " : " << it.value() << "\n"; }// 使用find函數(shù) if (o.find("foo") != o.end()) {// there is an entry with key "foo" }// count函數(shù) int foo_present = o.count("foo"); // 1 int fob_present = o.count("fob"); // 0// 刪除條目 o.erase("foo");?
五.從STL容器轉(zhuǎn)換
任何序列容器(std::array,std::vector,std::deque,std::forward_list,std::list),其值可以被用于構(gòu)建JSON類型(例如,整數(shù),浮點(diǎn)數(shù),布爾值,字符串類型,或者再次在本節(jié)中描述STL容器)可被用于創(chuàng)建JSON陣列。這同樣適用于類似的關(guān)聯(lián)容器(std::set,std::multiset,std::unordered_set,std::unordered_multiset),但是在這些情況下,陣列的元素的順序取決于元素是如何在各個(gè)STL容器排序。
std::vector<int> c_vector {1, 2, 3, 4}; json j_vec(c_vector); // [1, 2, 3, 4]std::deque<double> c_deque {1.2, 2.3, 3.4, 5.6}; json j_deque(c_deque); // [1.2, 2.3, 3.4, 5.6]std::list<bool> c_list {true, true, false, true}; json j_list(c_list); // [true, true, false, true]std::forward_list<int64_t> c_flist {12345678909876, 23456789098765, 34567890987654, 45678909876543}; json j_flist(c_flist); // [12345678909876, 23456789098765, 34567890987654, 45678909876543]std::array<unsigned long, 4> c_array {{1, 2, 3, 4}}; json j_array(c_array); // [1, 2, 3, 4]std::set<std::string> c_set {"one", "two", "three", "four", "one"}; json j_set(c_set); // only one entry for "one" is used // ["four", "one", "three", "two"]std::unordered_set<std::string> c_uset {"one", "two", "three", "four", "one"}; json j_uset(c_uset); // only one entry for "one" is used // maybe ["two", "three", "four", "one"]std::multiset<std::string> c_mset {"one", "two", "one", "four"}; json j_mset(c_mset); // both entries for "one" are used // maybe ["one", "two", "one", "four"]std::unordered_multiset<std::string> c_umset {"one", "two", "one", "four"}; json j_umset(c_umset); // both entries for "one" are used // maybe ["one", "two", "one", "four"]?
同樣,任何鍵值容器(std::map,std::multimap,std::unordered_map,std::unordered_multimap),其鍵可以構(gòu)造一個(gè)std::string,并且其值可以被用于構(gòu)建JSON類型(參見(jiàn)上文實(shí)施例)可用于創(chuàng)建一個(gè)JSON對(duì)象。請(qǐng)注意,在多重映射的情況下,JSON對(duì)象中僅使用一個(gè)鍵,該值取決于STL容器的內(nèi)部順序。
std::map<std::string, int> c_map { {"one", 1}, {"two", 2}, {"three", 3} }; json j_map(c_map); // {"one": 1, "three": 3, "two": 2 }std::unordered_map<const char*, double> c_umap { {"one", 1.2}, {"two", 2.3}, {"three", 3.4} }; json j_umap(c_umap); // {"one": 1.2, "two": 2.3, "three": 3.4}std::multimap<std::string, bool> c_mmap { {"one", true}, {"two", true}, {"three", false}, {"three", true} }; json j_mmap(c_mmap); // only one entry for key "three" is used // maybe {"one": true, "two": true, "three": true}std::unordered_multimap<std::string, bool> c_ummap { {"one", true}, {"two", true}, {"three", false}, {"three", true} }; json j_ummap(c_ummap); // only one entry for key "three" is used // maybe {"one": true, "two": true, "three": true}?
六.隱式轉(zhuǎn)換
JSON對(duì)象的類型由要存儲(chǔ)的表達(dá)式自動(dòng)確定。同樣,存儲(chǔ)的值被隱式轉(zhuǎn)換。
// strings std :: string s1 = “ Hello,world!” ; json js = s1; std :: string s2 = js;// Booleans bool b1 = true ; json jb = b1; bool b2 = jb;// numbers int i = 42 ; json jn = i; double f = jn;//...?
還可以明確指定轉(zhuǎn)換的類型:
std :: string vs = js.get <std :: string>(); bool vb = jb.get < bool >(); int vi = jn.get < int >();?
七.任意類型轉(zhuǎn)換
每個(gè)類型都可以以JSON序列化,而不僅僅是STL容器和標(biāo)量類型。通常情況下,你會(huì)做這些事情:
namespace ns {struct person {std::string name;std::string address;int age;}; }ns::person p = {"Ned Flanders", "744 Evergreen Terrace", 60};// 轉(zhuǎn)換為JSON:將每個(gè)值復(fù)制到JSON對(duì)象中 json j; j["name"] = p.name; j["address"] = p.address; j["age"] = p.age;// ...// 從JSON轉(zhuǎn)換:從JSON對(duì)象復(fù)制每個(gè)值 ns::person p {j["name"].get<std::string>(),j["address"].get<std::string>(),j["age"].get<int>() };?
或者,使用一種更簡(jiǎn)便的用法
// 創(chuàng)建一個(gè)person類并初始化 ns::person p {"Ned Flanders", "744 Evergreen Terrace", 60};// 隱式轉(zhuǎn)換: person -> json json j = p;std::cout << j << std::endl; // {"address":"744 Evergreen Terrace","age":60,"name":"Ned Flanders"}// 隱式轉(zhuǎn)換: json -> person ns::person p2 = j;// 測(cè)試是否完全相同 assert(p == p2);?
以上是對(duì)json for modern c++基本功能的使用總結(jié),如有哪里有誤,歡迎指出,相互學(xué)習(xí)交流。
總結(jié)
以上是生活随笔為你收集整理的json for modern c++,这是我用过的最好用的json库了的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 快手主页一键跳转添加微信
- 下一篇: python 离线翻译软件_简单翻译软件