json for modern c++,这是我用过的最好用的json库了
GitHub開源項的地址:https://github.com/nlohmann/json
json for modern c++是一個德國大牛nlohmann寫的,有以下特點:
1.直觀的語法。
2.整個代碼由一個頭文件組成json.hpp,沒有子項目,沒有依賴關系,沒有復雜的構建系統,使用起來非常方便。
3.使用c++11標準編寫。
4.使用json 像使用STL容器一樣。
5.STL和json容器之間可以相互轉換。
還有很多其他的特性,不一一列舉了。
如何使用?
將github上的src文件夾里的json.hpp頭文件下載保存到當前目錄中。
在代碼中包含json.hpp頭文件并引入json作用域
?
常用功能:
一.創建json對象
1.使用cin,cout輸入輸出流。
json提供了cin,cout的輸入輸出流的操作符。但需要注意的是,cin要有ctr + D結束輸入。cin會把從標準輸入的內容反序列化,cout會自動把json序列化,以string形式輸出。
2.提供根據鍵直接生成鍵值對的方法。(類似于map,如果不存在該鍵的話,就生成一個這個鍵。)
#include <iostream> #include "json.hpp" using json = nlohmann::json; using namespace std;int main() {// 創建一個json對象(null)json j;//添加一個存儲為double的數字j["pi"] = 3.141;// 添加一個布爾值 j["happy"] = true;// 添加一個存儲為std :: string的字符串j["name"] = "Niels";// 通過傳遞nullptr添加另一個空對象j["nothing"] = nullptr;// 在對象中添加對象j["answer"]["everything"] = 42;//添加一個數組,其存儲為std::vector(使用初始化列表)j["list"] = { 1, 0, 2 };// 在一個對象中添加另一個對象j["object"] = { {"currency", "USD"}, {"value", 42.99} };// 也可以通過直接賦值方式創建json對象,兩種方式創建結果相同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.在所有上述情況下,你不需要“告訴”編譯器要使用哪個JSON值。如果你想明確或表達一些邊緣的情況下,可以使用json::array,json::object。
#include <iostream> #include "json.hpp" using json = nlohmann::json; using namespace std;int main() {//創建一個空數組json empty_array_explicit = json::array();// 創建一個空對象的兩種方式json empty_object_implicit = json({});json empty_object_explicit = json::object(); }?
4.幾點注意
1).array是一個數組,可以用數字直接下標訪問。
?
2)數組中包含一個數組
json array = {{"a",6},"xin",8}; cout?<<?array[0][0]?<<?endl;??//輸出數組里的數組元素中的第一個元素的值?
3)數組中包含一個對象
json array = {{{"a",6}},"xin",8}; //輸出數組中第一個元素"a"對應的鍵值 cout?<<?array[0]["a"]?<<?endl;?
4).對象中嵌套一個對象和一個數組
json object = {{"a",{{"feng",6}}},{"b",{1,2,3}}};cout << object << endl; /*輸出: {"a":{"feng":6},"b":[1,2,3]} */?
二.序列化
將json對象序列化,成為字符串
1.標準輸出自動序列化
json j std :: cout << j; // setw操縱器被重載以設置漂亮打印的縮進 std :: cout << std :: setw( 4)<< j << std :: endl;?
2.使用dump()函數
//顯式轉換為string std::string s = j.dump(); // {\"happy\":true,\"pi\":3.141}//序列化與漂亮的打印 //傳入空格的數量縮進 std::cout << j.dump(4) << std::endl; // 輸出: //{ // "happy": true, // "pi": 3.141 // }?
三.反序列化
將數據流轉化為json對象
1.從標準輸入反序列化
json j std :: cin >> j;?
2.通過附加_json到字符串文字來創建對象(反序列化):
//從字符串文字創建對象 json j = " { \" happy \":true,\" pi \":3.141} " _json;//或者原始字符串文字 auto j2 = R"({ "happy":true,"pi":3.141 } )"?_json;?
請注意,沒有附加_json后綴,傳遞的字符串文字不會被解析,而只是用作JSON字符串值。也就是說,json j = "{ \"happy\": true, \"pi\": 3.141 }"只存儲字符串"{ "happy": true, "pi": 3.141 }"而不是解析實際的對象。
3.使用json::parse()函數
?
4.從迭代器范圍讀取
您還可以從迭代器范圍讀取JSON; 也就是說,可以從其內容存儲為連續字節序列的迭代器訪問的任何容器,例如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適應
nlohmann設計的JSON類,就像一個STL容器一樣。其實它滿足了可逆容器要求。
//使用push_back創建一個數組 json j; j.push_back("foo"); j.push_back(1); j.push_back(true);// 也可以使用emplace_back j.emplace_back(1.78);// 使用迭代器訪問 for (json::iterator it = j.begin(); it != j.end(); ++it) {std::cout << *it << '\n'; }// 基于范圍for循環 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// 其他函數 j.size(); // 3 j.empty(); // false j.type(); // json::value_t::array j.clear(); // 使數組再次為空// 方便類型檢查 j.is_null(); j.is_boolean(); j.is_number(); j.is_object(); j.is_array(); j.is_string();// 創建一個數組 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函數 if (o.find("foo") != o.end()) {// there is an entry with key "foo" }// count函數 int foo_present = o.count("foo"); // 1 int fob_present = o.count("fob"); // 0// 刪除條目 o.erase("foo");?
五.從STL容器轉換
任何序列容器(std::array,std::vector,std::deque,std::forward_list,std::list),其值可以被用于構建JSON類型(例如,整數,浮點數,布爾值,字符串類型,或者再次在本節中描述STL容器)可被用于創建JSON陣列。這同樣適用于類似的關聯容器(std::set,std::multiset,std::unordered_set,std::unordered_multiset),但是在這些情況下,陣列的元素的順序取決于元素是如何在各個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),其鍵可以構造一個std::string,并且其值可以被用于構建JSON類型(參見上文實施例)可用于創建一個JSON對象。請注意,在多重映射的情況下,JSON對象中僅使用一個鍵,該值取決于STL容器的內部順序。
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}?
六.隱式轉換
JSON對象的類型由要存儲的表達式自動確定。同樣,存儲的值被隱式轉換。
// 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;//...?
還可以明確指定轉換的類型:
std :: string vs = js.get <std :: string>(); bool vb = jb.get < bool >(); int vi = jn.get < int >();?
七.任意類型轉換
每個類型都可以以JSON序列化,而不僅僅是STL容器和標量類型。通常情況下,你會做這些事情:
namespace ns {struct person {std::string name;std::string address;int age;}; }ns::person p = {"Ned Flanders", "744 Evergreen Terrace", 60};// 轉換為JSON:將每個值復制到JSON對象中 json j; j["name"] = p.name; j["address"] = p.address; j["age"] = p.age;// ...// 從JSON轉換:從JSON對象復制每個值 ns::person p {j["name"].get<std::string>(),j["address"].get<std::string>(),j["age"].get<int>() };?
或者,使用一種更簡便的用法
// 創建一個person類并初始化 ns::person p {"Ned Flanders", "744 Evergreen Terrace", 60};// 隱式轉換: person -> json json j = p;std::cout << j << std::endl; // {"address":"744 Evergreen Terrace","age":60,"name":"Ned Flanders"}// 隱式轉換: json -> person ns::person p2 = j;// 測試是否完全相同 assert(p == p2);?
以上是對json for modern c++基本功能的使用總結,如有哪里有誤,歡迎指出,相互學習交流。
總結
以上是生活随笔為你收集整理的json for modern c++,这是我用过的最好用的json库了的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 快手主页一键跳转添加微信
- 下一篇: python 离线翻译软件_简单翻译软件