json的序列化与反序列化

该文介绍了如何在C++环境中使用nlohmann/json库进行JSON对象的序列化和反序列化,包括基本对象赋值、转换为字符串以及从字符串恢复JSON对象。示例代码展示了json::dump()和json::parse()函数的用法。
摘要由CSDN通过智能技术生成

目录

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<iostream>
#include<vector>
#include<string>
#include<map>
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的反序列化

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

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

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

#include<iostream>
#include<vector>
#include<string>
#include<map>
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<int> 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<string, string> 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<int> vec = js["arr"];
    for(auto &a : vec) cout << a << " ";
    cout << endl << endl;

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

    cout << "反序列化示例4" << endl;
    map<string, string> 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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 中的 JSON 序列化反序列化可以使用许多第三方库来实现,比如 Jackson、Gson、FastJson 等。这里以 Jackson 库为例,介绍如何进行 JSON 序列化反序列化。 ### Jackson 序列化 Jackson 序列化可以将 Java 对象转换为 JSON 字符串。示例代码如下: ```java ObjectMapper objectMapper = new ObjectMapper(); MyObject myObject = new MyObject(); // 要序列化的 Java 对象 String json = objectMapper.writeValueAsString(myObject); // 序列化JSON 字符串 ``` 在上面的代码中,ObjectMapper 是 Jackson 序列化反序列化的核心类。writeValueAsString() 方法将 Java 对象序列化JSON 字符串。 如果要对序列化后的 JSON 字符串进行格式化,可以使用 ObjectMapper 的 writerWithDefaultPrettyPrinter() 方法: ```java ObjectMapper objectMapper = new ObjectMapper(); MyObject myObject = new MyObject(); // 要序列化的 Java 对象 String json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(myObject); // 序列化成格式化后的 JSON 字符串 ``` ### Jackson 反序列化 Jackson 反序列化可以将 JSON 字符串转换为 Java 对象。示例代码如下: ```java ObjectMapper objectMapper = new ObjectMapper(); String json = "{\"name\":\"Tom\", \"age\":20}"; // 要反序列化JSON 字符串 MyObject myObject = objectMapper.readValue(json, MyObject.class); // 反序列化成 Java 对象 ``` 在上面的代码中,readValue() 方法将 JSON 字符串反序列化成 Java 对象。第一个参数是要反序列化JSON 字符串,第二个参数是目标 Java 类型。 ### Jackson 注解 Jackson 序列化反序列化还支持一些注解,用于控制序列化反序列化的行为。比如,可以使用 @JsonProperty 注解指定 JSON 字段名: ```java public class MyObject { @JsonProperty("user_name") private String name; private int age; // ... } ``` 在上面的代码中,@JsonProperty("user_name") 注解指定了 JSON 字段名为 "user_name",而不是默认的 "name"。这样,在序列化反序列化时,Jackson 就会使用指定的字段名。 除了 @JsonProperty 注解,Jackson 还支持许多其他注解,比如 @JsonIgnore、@JsonFormat、@JsonInclude 等。这些注解可以用于控制序列化反序列化的行为,更多的注解用法可以参考 Jackson 的官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值