可以直接赋值一个容器对象
js[‘xx’]=vec;
#include "json.hpp"
using json=nlohmann::json;
#include <iostream>
#include <vector>
#include <map>
using namespace std;
//json序列化示例1
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;
}
int main(){
func1();
return 0;
}
转成字符串,就可以通过网络发送
#include "json.hpp"
using json=nlohmann::json;
#include <iostream>
#include <vector>
#include <map>
#include <string>
using namespace std;
//json序列化示例1
void func1(){
json js;
js["msg_type"]=2;
js["from"]="zhang san";
js["to"]="li si";
js["msg"]="hello,what are you doing now?";
string sendBuf=js.dump();
cout<<sendBuf.c_str()<<endl;
}
int main(){
func1();
return 0;
}
还可以放数组,json形式还可以像是二维数组一样,键值对里面套键值对
#include "json.hpp"
using json=nlohmann::json;
#include <iostream>
#include <vector>
#include <map>
#include <string>
using namespace std;
//json序列化示例1
void func1(){
json js;
js["id"]={1,2,3,4,5};
js["msg_type"]=2;
js["from"]="zhang san";
js["to"]="li si";
js["msg"]["zhang san"]="hello world";
js["msg"]["liu shuo"]="hello china";
cout<<js<<endl;
}
int main(){
func1();
return 0;
}