示例一
简单信息交互序列化:
#include "json.hpp"
using json = nlohmann::json;
#include <iostream>
#include <vector>
#include <map>
using namespace std;
void func1() {
json js;
js["msg_type"] = 2;
js["from"] = "zhang san";
js["to"] = "li si";
js["msg"] = "hello, what are you doing now?";
// cout << js << endl;
// json数据对象转换为序列化json字符串
string sendBuf = js.dump();
cout << sendBuf.c_str() << endl;
}
int main() {
func1();
return 0;
}
反序列化:
#include "json.hpp"
using json = nlohmann::json;
#include <iostream>
#include <vector>
#include <map>
using namespace std;
string func11() {
json js;
js["msg_type"] = 2;
js["from"] = "zhang san";
js["to"] = "li si";
js["msg"] = "hello, what are you doing now?";
// cout << js << endl;
// json数据对象转换为序列化json字符串
string sendBuf = js.dump();
return sendBuf;
}
int main() {
string recvBuf = func11();
// 反序列化json字符串
json jsbuf = json::parse(recvBuf);
cout << jsbuf["msg_type"] << endl;
cout << jsbuf["from"] << endl;
cout << jsbuf["to"] << endl;
cout << jsbuf["msg"] << endl;
}
示例二
带有数组信息序列化:
#include "json.hpp"
using json = nlohmann::json;
#include <iostream>
#include <vector>
#include <map>
using namespace std;
void func2() {
json js;
js["id"] = {1,2,3,4,5};
js["name"] = "zhang san";
js["msg"]["zhang san"] = "hello world";
js["msg"]["liu shou"] = "hello china";
// js["msg"] = {{"zhang san", "hello world"}, {"liu shou", "hello china"}};
string sendBuf = js.dump();
cout << sendBuf << endl;
}
int main() {
func2();
return 0;
}
反序列化:
#include "json.hpp"
using json = nlohmann::json;
#include <iostream>
#include <vector>
#include <map>
using namespace std;
string func22() {
json js;
js["id"] = {1,2,3,4,5};
js["name"] = "zhang san";
js["msg"]["zhang san"] = "hello world";
js["msg"]["liu shou"] = "hello china";
// js["msg"] = {{"zhang san", "hello world"}, {"liu shou", "hello china"}};
string sendBuf = js.dump();
return sendBuf;
}
int main() {
string recvBuf = func22();
// 反序列化json字符串
json jsbuf = json::parse(recvBuf);
cout << jsbuf["id"] << endl;
auto arr = jsbuf["id"];
cout << arr[2] << endl;
auto msgjs = jsbuf["msg"];
cout << msgjs["zhang san"] << endl;
cout << msgjs["liu shou"] << endl;
return 0;
}
示例三
vector容器和map容器序列化:
#include "json.hpp"
using json = nlohmann::json;
#include <iostream>
#include <vector>
#include <map>
using namespace std;
void func3() {
json js;
// 序列化一个vector容器
vector<int> vec;
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
js["list"] = vec;
// 序列化一个map容器
map<int, string> m;
m.insert({1, "黄山"});
m.insert({2, "华山"});
m.insert({3, "泰山"});
js["path"] = m;
string sendBuf = js.dump();
cout << sendBuf << endl;
}
int main() {
func3();
return 0;
}
反序列化:
#include "json.hpp"
using json = nlohmann::json;
#include <iostream>
#include <vector>
#include <map>
using namespace std;
string func33() {
json js;
// 反序列化一个vector容器
vector<int> vec;
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
js["list"] = vec;
// 反序列化一个map容器
map<int, string> m;
m.insert({1, "黄山"});
m.insert({2, "华山"});
m.insert({3, "泰山"});
js["path"] = m;
string sendBuf = js.dump();
return sendBuf;
}
int main() {
string recvBuf = func22();
// 反序列化json字符串
json jsbuf = json::parse(recvBuf);
vector<int> vec = jsbuf["list"];
for(int &v : vec){
cout << v << " ";
}
cout << endl;
map<int, string> mymap = jsbuf["path"];
for(auto &p : mymap){
cout << p.first << " " << p.second << endl;
}
cout << endl;
return 0;
}