#include "json.hpp" using json = nlohmann::json; #include #include #include #include using namespace std; //json序列化示例1 void func1() { json js;//定义一个json类型的对象//添加数组 js["msg_type"] = 2; js["from"] = "linzeyu"; js["to"] = "zhang san"; js["msg"] = "hello, are you ok now?"; cout< 保存,运行。 数组序列化 json的键可以是整数类型,字符串类型,还可以是数组类型。 #include "json.hpp" using json = nlohmann::json; #include #include #include #include using namespace std; //json序列化示例1 string func1() { json js;//定义一个json类型的对象//添加数组 js["msg_type"] = 2; js["from"] = "linzeyu"; js["to"] = "zhang san"; js["msg"] = "hello, are you ok now?"; string sendBuf = js.dump();//输出的意思 //cout< 运行。 容器序列化 JSON for Modern C++ 可以把C++ STL中的容器内容可以直接序列化成Json字符串。 #include "json.hpp" using json = nlohmann::json; #include #include #include #include using namespace std; //json序列化示例1 string func1() { json js;//定义一个json类型的对象//添加数组 js["msg_type"] = 2; js["from"] = "linzeyu"; js["to"] = "zhang san"; js["msg"] = "hello, are you ok now?"; string sendBuf = js.dump();//输出的意思 //cout< vec; vec.push_back(1); vec.push_back(2); vec.push_back(5); js["list"] = vec; //直接序列化一个map容器 map m; m.insert({1, "黄山"}); m.insert({2, "华山"}); m.insert({3, "泰山"}); js["path"] = m; //string sendBuf = js.dump(); // json数据对象 =》序列化 json字符串 cout< 运行。 数据反序列化 当从网络接收到字符串为Json格式,可以用JSON for Modern C++ 直接反序列化取得数据或者直接反序列化出对象或者容器。 上述三个函数的反序列化如下所示: #include "json.hpp" using json = nlohmann::json; #include #include #include #include using namespace std; //json序列化示例1 string func1() { json js;//定义一个json类型的对象//添加数组 js["msg_type"] = 2; js["from"] = "linzeyu"; js["to"] = "zhang san"; js["msg"] = "hello, are you ok now?"; string sendBuf = js.dump();//输出的意思 //cout< vec; vec.push_back(1); vec.push_back(2); vec.push_back(5); js["list"] = vec; //直接序列化一个map容器 map m; m.insert({1, "黄山"}); m.insert({2, "华山"}); m.insert({3, "泰山"}); js["path"] = m; string sendBuf = js.dump(); //json数据对象 =》序列化 json字符串 //cout< vec = jsbuf["list"]; //js对象里面的数组类型,直接放入vector容器当中 //for (int &v : vec) //{ // cout << v << " "; //} //cout << endl; //map mymap = jsbuf["path"]; //for (auto &p : mymap) //{ // cout << p.first << " " << p.second << endl; //} //cout << endl; return 0; }
保存,运行。
json的键可以是整数类型,字符串类型,还可以是数组类型。
#include "json.hpp" using json = nlohmann::json; #include #include #include #include using namespace std; //json序列化示例1 string func1() { json js;//定义一个json类型的对象//添加数组 js["msg_type"] = 2; js["from"] = "linzeyu"; js["to"] = "zhang san"; js["msg"] = "hello, are you ok now?"; string sendBuf = js.dump();//输出的意思 //cout< 运行。 容器序列化 JSON for Modern C++ 可以把C++ STL中的容器内容可以直接序列化成Json字符串。 #include "json.hpp" using json = nlohmann::json; #include #include #include #include using namespace std; //json序列化示例1 string func1() { json js;//定义一个json类型的对象//添加数组 js["msg_type"] = 2; js["from"] = "linzeyu"; js["to"] = "zhang san"; js["msg"] = "hello, are you ok now?"; string sendBuf = js.dump();//输出的意思 //cout< vec; vec.push_back(1); vec.push_back(2); vec.push_back(5); js["list"] = vec; //直接序列化一个map容器 map m; m.insert({1, "黄山"}); m.insert({2, "华山"}); m.insert({3, "泰山"}); js["path"] = m; //string sendBuf = js.dump(); // json数据对象 =》序列化 json字符串 cout< 运行。 数据反序列化 当从网络接收到字符串为Json格式,可以用JSON for Modern C++ 直接反序列化取得数据或者直接反序列化出对象或者容器。 上述三个函数的反序列化如下所示: #include "json.hpp" using json = nlohmann::json; #include #include #include #include using namespace std; //json序列化示例1 string func1() { json js;//定义一个json类型的对象//添加数组 js["msg_type"] = 2; js["from"] = "linzeyu"; js["to"] = "zhang san"; js["msg"] = "hello, are you ok now?"; string sendBuf = js.dump();//输出的意思 //cout< vec; vec.push_back(1); vec.push_back(2); vec.push_back(5); js["list"] = vec; //直接序列化一个map容器 map m; m.insert({1, "黄山"}); m.insert({2, "华山"}); m.insert({3, "泰山"}); js["path"] = m; string sendBuf = js.dump(); //json数据对象 =》序列化 json字符串 //cout< vec = jsbuf["list"]; //js对象里面的数组类型,直接放入vector容器当中 //for (int &v : vec) //{ // cout << v << " "; //} //cout << endl; //map mymap = jsbuf["path"]; //for (auto &p : mymap) //{ // cout << p.first << " " << p.second << endl; //} //cout << endl; return 0; }
运行。
JSON for Modern C++ 可以把C++ STL中的容器内容可以直接序列化成Json字符串。
#include "json.hpp" using json = nlohmann::json; #include #include #include #include using namespace std; //json序列化示例1 string func1() { json js;//定义一个json类型的对象//添加数组 js["msg_type"] = 2; js["from"] = "linzeyu"; js["to"] = "zhang san"; js["msg"] = "hello, are you ok now?"; string sendBuf = js.dump();//输出的意思 //cout< vec; vec.push_back(1); vec.push_back(2); vec.push_back(5); js["list"] = vec; //直接序列化一个map容器 map m; m.insert({1, "黄山"}); m.insert({2, "华山"}); m.insert({3, "泰山"}); js["path"] = m; //string sendBuf = js.dump(); // json数据对象 =》序列化 json字符串 cout< 运行。 数据反序列化 当从网络接收到字符串为Json格式,可以用JSON for Modern C++ 直接反序列化取得数据或者直接反序列化出对象或者容器。 上述三个函数的反序列化如下所示: #include "json.hpp" using json = nlohmann::json; #include #include #include #include using namespace std; //json序列化示例1 string func1() { json js;//定义一个json类型的对象//添加数组 js["msg_type"] = 2; js["from"] = "linzeyu"; js["to"] = "zhang san"; js["msg"] = "hello, are you ok now?"; string sendBuf = js.dump();//输出的意思 //cout< vec; vec.push_back(1); vec.push_back(2); vec.push_back(5); js["list"] = vec; //直接序列化一个map容器 map m; m.insert({1, "黄山"}); m.insert({2, "华山"}); m.insert({3, "泰山"}); js["path"] = m; string sendBuf = js.dump(); //json数据对象 =》序列化 json字符串 //cout< vec = jsbuf["list"]; //js对象里面的数组类型,直接放入vector容器当中 //for (int &v : vec) //{ // cout << v << " "; //} //cout << endl; //map mymap = jsbuf["path"]; //for (auto &p : mymap) //{ // cout << p.first << " " << p.second << endl; //} //cout << endl; return 0; }
当从网络接收到字符串为Json格式,可以用JSON for Modern C++ 直接反序列化取得数据或者直接反序列化出对象或者容器。
上述三个函数的反序列化如下所示:
#include "json.hpp" using json = nlohmann::json; #include #include #include #include using namespace std; //json序列化示例1 string func1() { json js;//定义一个json类型的对象//添加数组 js["msg_type"] = 2; js["from"] = "linzeyu"; js["to"] = "zhang san"; js["msg"] = "hello, are you ok now?"; string sendBuf = js.dump();//输出的意思 //cout< vec; vec.push_back(1); vec.push_back(2); vec.push_back(5); js["list"] = vec; //直接序列化一个map容器 map m; m.insert({1, "黄山"}); m.insert({2, "华山"}); m.insert({3, "泰山"}); js["path"] = m; string sendBuf = js.dump(); //json数据对象 =》序列化 json字符串 //cout< vec = jsbuf["list"]; //js对象里面的数组类型,直接放入vector容器当中 //for (int &v : vec) //{ // cout << v << " "; //} //cout << endl; //map mymap = jsbuf["path"]; //for (auto &p : mymap) //{ // cout << p.first << " " << p.second << endl; //} //cout << endl; return 0; }
上一篇 (C语言之路-----p2:函数)
下一篇 刷洛谷/LintCode/牛客刷题学习心得(1)(c语言查漏补缺)
版权所有 (c)2021-2022 MSHXW.COM
ICP备案号:晋ICP备2021003244-6号