json的序列化与反序列化

目录

json的下载

json的序列化

json的反序列化


备注json开源项目github地址:https://github.com/nlohmann/json

备注开发环境:vscode通过ssh连接虚拟机中的ubuntu,ubuntu-20.04.3-desktop-amd64.iso

json的下载

git clone https://github.com/nlohmann/json.git

json的序列化

dump()方法将json对象转为string类型

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

#include
#include
#include
#include
using namespace std;

void func1()
{
    json js;
    js["msg_type"] = 2;
    js["from"] = "zhang san";
    js["to"] = "li si";
    js["msg"] = "hello!";

    cout << "序列化示例1" << endl;
    cout << js << endl;
    string sendBuf = js.dump();
    cout << sendBuf.c_str() << endl;
}

void func2()
{
    json js;
    js["id"] = {1,2,3,4,5};
    js["name"] = "zhang san";

    js["msg"]["zhang san"] = "hello world!";
    js["msg"]["li si"] = "Hello China!";
    // 以上两条代码等价于:
    // js["msg"] = {{"zhang san","hello world!"}, {"li si","Hello China!"}};

    cout << "序列化示例2" << endl;
    cout << js << endl;
    string sendBuf = js.dump();
    cout << sendBuf.c_str() << endl;
}

int main()
{
    func1();
    cout << endl;
    func2();
    
    return 0;
}

 g++ json_1.cpp -o json_1 -I ./json/include

./json_1

json的序列化与反序列化_第1张图片

json的反序列化

dump()方法将json对象转为string类型 

json::parse()将string类型转为json对象

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

#include
#include
#include
#include
using namespace std;

string func1()
{
    json js;
    js["msg_type"] = 2;
    js["from"] = "zhang san";
    js["to"] = "li si";
    js["msg"] = "hello!";

    string sendBuf = js.dump();
    // json对象转为string类型

    return sendBuf;
}

string func2()
{
    json js;
    js["id"] = {1,2,3,4,5};
    js["name"] = "zhang san";
    js["msg"]["zhang san"] = "hello world!";
    js["msg"]["li si"] = "Hello China!";

    string sendBuf = js.dump();
    // json对象转为string类型

    return sendBuf;
}

// vector序列化
string func3()
{
    json js;
    vector vec;

    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(3);

    js["arr"] = vec;
    string sendBuf = js.dump();
    // json对象转为string类型

    return sendBuf;
}

// map序列化
string func4()
{
    json js;
    map m;

    m["name"] = "yang";
    m["gender"] = "boy";

    js["info"] = m;
    string sendBuf = js.dump();
    // json对象转为string类型

    return sendBuf;
}

int main()
{
    string recvBuf = func1();
    json js = json::parse(recvBuf);
    // string类型转为json对象

    cout << "反序列化示例1" << endl;
    cout << js["msg_type"] << endl;
    cout << js["from"] << endl;
    cout << js["to"] << endl;
    cout << js["msg"] << endl << endl;

    recvBuf = func2();
    js = json::parse(recvBuf);
    // string类型转为json对象

    cout << "反序列化示例2" << endl;
    cout << js["id"] << endl;
    for(auto &a : js["id"]) cout << a << " ";
    cout << endl;
    cout << js["name"] << endl;
    cout << js["msg"]["zhang san"] << endl;
    cout << js["msg"]["li si"] << endl << endl;

    recvBuf = func3();
    js = json::parse(recvBuf);
    // string类型转为json对象

    cout << "反序列化示例3" << endl;
    vector vec = js["arr"];
    for(auto &a : vec) cout << a << " ";
    cout << endl << endl;

    recvBuf = func4();
    js = json::parse(recvBuf);
    // string类型转为json对象

    cout << "反序列化示例4" << endl;
    map m = js["info"];
    cout << m["name"] << endl;
    cout << m["gender"] << endl << endl;

    return 0;
}

 g++ json_2.cpp -o json_2 -I ./json/include

./json_2

json的序列化与反序列化_第2张图片

你可能感兴趣的:(拓展学习,json)