• 使用nlohmann json库进行序列化与反序列化


    #include 
    #include "nlohmann/json.hpp"
    #include 
    
    using json = nlohmann::json;
    
    // 定义嵌套的结构体
    struct Address
    {
        std::string street;
        std::string city;
    };
    
    // 定义包含多种数据类型和结构体数组的主结构体
    struct Person
    {
        std::string name;
        int age;
        bool isStudent;
        std::vector<int> grades;
        Address address;
        std::vector<Address> addresses;
    };
    
    int main()
    {
        // 创建一个Person结构体实例
        Person person;
        person.name = "John";
        person.age = 30;
        person.isStudent = false;
        person.grades = {90, 85, 92};
        person.address = {"123 Main St", "Anytown"};
        person.addresses = {{"456 Elm St", "Somewhere"}, {"789 Oak St", "Nowhere"}};
    
        // 序列化结构体到JSON
        json jsonData = {
            {"name", person.name},
            {"age", person.age},
            {"isStudent", person.isStudent},
            {"grades", person.grades},
            {"address", {{"street", person.address.street}, {"city", person.address.city}}},
            {"addresses", json::array()}};
    
            jsonData["age"] = 20;
    
        for (const auto &addr : person.addresses)
        {
            jsonData["addresses"].push_back({{"street", addr.street},
                                             {"city", addr.city}});
        }
    
        // 将JSON对象转换为字符串
        std::string jsonString = jsonData.dump(4); // 4-space indentation for pretty formatting
    
        // 打印生成的JSON字符串
        std::cout << "Generated JSON:" << std::endl;
        std::cout << jsonString << std::endl;
    
        // 反序列化JSON字符串到结构体
        Person parsedPerson;
        json parsedData = json::parse(jsonString);
    
        parsedPerson.name = parsedData["name"];
        parsedPerson.age = parsedData["age"];
        parsedPerson.isStudent = parsedData["isStudent"];
        std::vector<int> parsedGrades = parsedData["grades"].get<std::vector<int>>();
        parsedPerson.address.street = parsedData["address"]["street"];
        parsedPerson.address.city = parsedData["address"]["city"];
    
        parsedPerson.addresses.clear();
        for (const auto &addr : parsedData["addresses"])
        {
            Address parsedAddr;
            parsedAddr.street = addr["street"];
            parsedAddr.city = addr["city"];
            parsedPerson.addresses.push_back(parsedAddr);
        }
    
        // 打印反序列化后的结构体成员
        std::cout << "Name: " << parsedPerson.name << std::endl;
        std::cout << "Age: " << parsedPerson.age << std::endl;
        std::cout << "Is Student: " << parsedPerson.isStudent << std::endl;
        std::cout << "Grades: ";
        for (int grade : parsedPerson.grades)
        {
            std::cout << grade << " ";
        }
        std::cout << std::endl;
    
        std::cout << "Address: Street: " << parsedPerson.address.street << ", City: " << parsedPerson.address.city << std::endl;
    
        std::cout << "Addresses:" << std::endl;
        for (const auto &addr : parsedPerson.addresses)
        {
            std::cout << "Street: " << addr.street << ", City: " << addr.city << std::endl;
        }
    
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
  • 相关阅读:
    好库推荐|两个解决ja3检测的Python库,强烈推荐
    【Python】成功解决TypeError: list indices must be integers or slices, not float
    NNZ 2591 是环状甘氨酸脯氨酸 (cGP) 小肽的合成类似物 | MedChemExpress (MCE)
    22、Flink 解决数据倾斜问题
    Delphi 的操作符重载 - Record 结构体
    我,PolarDB云原生数据库,5年来实现这些重磅技术创新
    MYSQL根据FRM文件导出表结构
    Java控制结构总结[顺序控制 分支控制 循环控制]
    python经典百题之画椭圆
    Spring Cloud Alibaba 分布式微服务高并发数据平台化(中台)思想+多租户saas设计的企业开发架构
  • 原文地址:https://blog.csdn.net/c1724136570/article/details/132644466