集群聊天服务器(2)Json介绍

目录

大家之间交流用json,想要发送数据,就把数据序列化成json,想要接收数据,就反序列化成自己程序的语言。

Json序列化

可以直接赋值一个容器对象

js['xx']=vec;

cpp 复制代码
#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;
}

转成字符串,就可以通过网络发送

cpp 复制代码
#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形式还可以像是二维数组一样,键值对里面套键值对

cpp 复制代码
#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;
}

Json反序列化

cpp 复制代码
#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;
}

string func2(){
    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";
    string sendBuf=js.dump();
    return sendBuf;

}

int main(){
    string recvBuf=func2();
    //数据的反序列化
    json jsbuf=json::parse(recvBuf);//返回一个json对象
    cout<<jsbuf["msg_type"]<<endl;
    cout<<jsbuf["from"]<<endl;
    cout<<jsbuf["to"]<<endl;
    cout<<jsbuf["msg"]<<endl;

    return 0;
}

json.hpp非常轻量,比任何第三方库都好用。做json的序列化和反序列化,能够直接将stl容器和json画等号。

相关推荐
两点王爷23 分钟前
docker 运行自定义化的服务-后端
运维·docker·容器
邪恶的贝利亚1 小时前
FFMEPG常见命令查询
linux·运维·网络·ffmpeg
搜搜秀1 小时前
find指令中使用正则表达式
linux·运维·服务器·正则表达式·bash
七七powerful3 小时前
使用opentelemetry 可观测监控springboot应用的指标、链路实践,使用zipkin展示链路追踪数据,使用grafana展示指标
运维
Archie_IT3 小时前
修图自由!自建IOPaint服务器,手机平板随时随地远程调用在线P图
运维·服务器·前端·git·深度学习·npm·conda
行思理3 小时前
centos crontab 设置定时任务访问链接
linux·运维·centos
无名之逆3 小时前
[特殊字符] Hyperlane:为现代Web服务打造的高性能Rust文件上传解决方案
服务器·开发语言·前端·网络·后端·http·rust
再玩一会儿看代码4 小时前
[特殊字符] 深入理解 WSL2:在 Windows 上运行 Linux 的极致方案
linux·运维·windows·经验分享·笔记·学习方法
我是小木鱼5 小时前
浅析Centos7安装Oracle12数据库
linux·运维·服务器·数据库
Pluto & Ethereal6 小时前
新手宝塔部署thinkphp一步到位
运维·服务器·阿里云·php·腾讯云