json序列化和反序列化代码演示

示例一

简单信息交互序列化:

#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;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值