cpp struct json相互转换

C++结构体与JSON的相互转换

在现代软件开发中,数据的序列化和反序列化是一个常见的需求。尤其在客户端和服务器之间的数据交换中,JSON因其简单、易读和良好的兼容性而被广泛使用。本文将介绍如何在C++中实现结构体和JSON之间的相互转换,重点介绍使用 nlohmann/json库。

nlohmann/json库简介

nlohmann/json是一个流行的开源JSON库,支持C++11及以上版本。它提供了直观和简洁的接口,能够轻松地将C++对象与JSON数据相互转换。

安装nlohmann/json库

可以通过以下几种方式安装 nlohmann/json库:

  1. 使用包管理器(如vcpkg或Conan):

    vcpkg install nlohmann-json
    ​
    
  2. 手动下载库文件,并将 json.hpp包含到你的项目中:

    #include "json.hpp"
    ​
    

C++结构体与JSON的相互转换

定义结构体

首先,定义一个C++结构体,例如:

struct Person {
    std::string name;
    int age;
    std::string address;
};
​
转换函数

为了实现结构体与JSON的相互转换,需要定义两个函数:一个用于序列化(结构体转JSON),另一个用于反序列化(JSON转结构体)。

#include <iostream>
#include <string>
#include "json.hpp"

using json = nlohmann::json;

struct Person {
    std::string name;
    int age;
    std::string address;
};

// 序列化:结构体 -> JSON
void to_json(json& j, const Person& p) {
    j = json{
  {"name", p.name}, {"age", p.age}, {"address", p.address}};
}

// 反序列化:JSON -> 结构体
void from_json(const json& j, Person& p) {
    j.at("name").get_to(p.name);
    j.at("age").get_to(p.age);
    j.at("address").get_to(p.address);
}
​
使用示例
int main() {
    // 创建一个Person对象
    Person person = {"John Doe", 30, "123 Main St"};

    // 将Person对象序列化为JSON
    json j = person;
    std::cout << "Serialized JSON: " << j << std::endl;

    // 将JSON反序列化为Person对象
    Person new_person = j.get<Person>();
    std::cout << "Deserialized Person: " << new_person.name << ", " << new_person.age << ", " << new_person.address << std::endl;

    return 0;
}
​

处理复杂结构体

如果结构体包含复杂数据类型或嵌套结构体,可以使用相同的方法进行处理。以下是一个示例:

struct Address {
    std::string city;
    std::string street;
};

struct Employee {
    std::string name;
    int age;
    Address address;
};

// 序列化:结构体 -> JSON
void to_json(json& j, const Address& a) {
    j = json{
  {"city", a.city}, {"street", a.street}};
}

void to_json(json& j, const Employee& e) {
    j = json{
  {"name", e.name}, {"age", e.age}, {"address", e.address}};
}

// 反序列化:JSON -> 结构体
void from_json(const json& j, Address& a) {
    j.at("city").get_to(a.city);
    j.at("street").get_to(a.street);
}

void from_json(const json& j, Employee& e) {
    j.at("name").get_to(e.name);
    j.at("age").get_to(e.age);
    j.at("address").get_to(e.address);
}

int main() {
    // 创建一个Employee对象
    Address address = {"New York", "5th Avenue"};
    Employee employee = {"Alice", 28, address};

    // 将Employee对象序列化为JSON
    json j = employee;
    std::cout << "Serialized JSON: " << j << std::endl;

    // 将JSON反序列化为Employee对象
    Employee new_employee = j.get<Employee>();
    std::cout << "Deserialized Employee: " << new_employee.name << ", " << new_employee.age << ", " << new_employee.address.city << ", " << new_employee.address.street << std::endl;

    return 0;
}
​
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值