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;//定义一个json类型的对象//添加数组
    js["msg_type"] = 2;
    js["from"] = "linzeyu";
    js["to"] = "zhang san";
    js["msg"] = "hello, are you ok now?";
    
    cout<<js<<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
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<<sendBuf.c_str()<<endl;//转成char*
    return sendBuf;
}

//json序列化示例2
void func2()
{
    json js;
    //添加数组
    js["id"] = {1, 2, 3, 4, 5};
    //添加key-value
    js["name"] = "zhang san";
    //添加对象
    js["msg"]["zhang san"] = "hello world";
    js["msg"]["liu shuo"] = "hello china";
    //上面等同于下面这句一次性添加数组对象
    js["msg"] = {{"zhang san", "hello world"}, {"liu shuo", "hello china"}};
    cout << js << endl;
    //return js.dump();
}

int main()
{
    //func1();
    func2();
    return 0;
}

运行。

在这里插入图片描述

容器序列化

JSON for Modern C++ 可以把C++ STL中的容器内容可以直接序列化成Json字符串。

#include "json.hpp"
using json = nlohmann::json;

#include <iostream>
#include <vector>
#include <map>
#include <string>
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<<sendBuf.c_str()<<endl;//转成char*
    return sendBuf;
}

//json序列化示例2
string func2()
{
    json js;
    //添加数组
    js["id"] = {1, 2, 3, 4, 5};
    //添加key-value
    js["name"] = "zhang san";
    //添加对象
    js["msg"]["zhang san"] = "hello world";
    js["msg"]["liu shuo"] = "hello china";
    //上面等同于下面这句一次性添加数组对象
    js["msg"] = {{"zhang san", "hello world"}, {"liu shuo", "hello china"}};
    //cout << js << endl;
    return js.dump();
}

//json序列化示例代码3
void func3()
{
    json js;

    //直接序列化一个vector容器
    vector<int> vec;
    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(5);

    js["list"] = vec;

    //直接序列化一个map容器
    map<int, string> m;
    m.insert({1, "黄山"});
    m.insert({2, "华山"});
    m.insert({3, "泰山"});

    js["path"] = m;

    //string sendBuf = js.dump(); // json数据对象 =》序列化 json字符串
    cout<<js<<endl;
    //return sendBuf;
}
int main()
{
    //func1();
    //unc2();
    func3();
    return 0;
}

运行。
在这里插入图片描述

数据反序列化

当从网络接收到字符串为Json格式,可以用JSON for Modern C++ 直接反序列化取得数据或者直接反序列化出对象或者容器。

上述三个函数的反序列化如下所示:

#include "json.hpp"
using json = nlohmann::json;

#include <iostream>
#include <vector>
#include <map>
#include <string>
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<<sendBuf.c_str()<<endl;//转成char*
    return sendBuf;
}

//json序列化示例2
string func2()
{
    json js;
    //添加数组
    js["id"] = {1, 2, 3, 4, 5};
    //添加key-value
    js["name"] = "zhang san";
    //添加对象
    js["msg"]["zhang san"] = "hello world";
    js["msg"]["liu shuo"] = "hello china";
    //上面等同于下面这句一次性添加数组对象
    js["msg"] = {{"zhang san", "hello world"}, {"liu shuo", "hello china"}};
    //cout << js << endl;
    return js.dump();
}

//json序列化示例代码3
string func3()
{
    json js;

    //直接序列化一个vector容器
    vector<int> vec;
    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(5);

    js["list"] = vec;

    //直接序列化一个map容器
    map<int, string> m;
    m.insert({1, "黄山"});
    m.insert({2, "华山"});
    m.insert({3, "泰山"});

    js["path"] = m;

    string sendBuf = js.dump(); //json数据对象 =》序列化 json字符串
    //cout<<sendBuf<<endl;
    return sendBuf;
}

int main()
{
    //func1();
    //unc2();
    //func3();
    string recvBuf = func1();
    //string recvBuf = func2();
    //string recvBuf = func3();
    
    //数据的反序列化   json字符串 =》反序列化 数据对象(看作容器,方便访问)
    json jsbuf = json::parse(recvBuf);
    
    cout<<jsbuf["msg_type"]<<endl;
    cout<<jsbuf["from"]<<endl;
    cout<<jsbuf["to"]<<endl;
    cout<<jsbuf["msg"]<<endl;

    //cout<<jsbuf["id"]<<endl;
    //auto arr = jsbuf["id"];
    //cout<<arr[2]<<endl;

    //auto msgjs = jsbuf["msg"];
    //cout<<msgjs["zhang san"]<<endl;
    //cout<<msgjs["liu shuo"]<<endl;

    //vector<int> vec = jsbuf["list"]; //js对象里面的数组类型,直接放入vector容器当中
    //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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值