Qt_C++JSON转结构体

文章目录[隐藏]

Qt_C++++JSON 转结构体前言

最近在研究 Redis 的是时候,想着传输数据用 JSON 数据格式。但是Qt自带的 QJsonDocument 方式其实也是可以用的,但是我嫌弃(lan)读取 dom 的方式麻烦,
于是网上搜索了一个库来用,做了一些测试,基本功能已经满足,但是还没有深入用,不知道有没有什么坑,不扯远了,有没有坑用了就知道。
今天先给大家分享一个我今天做的 demo,可以实现一些基本数据的读取。

Qt_C++++JSON 转结构体需求说明

我想写入的 JSON 数据格式如下:
{
    "name": "C 加加",
    "master": 2020,
    "members": [{
        "id": 1,
        "name": "xiaohua",
        "mail": "xiaohua@qq.com"
    }, {
        "id": 2,
        "name": "xiaohe",
        "mail": "xiaohe@qq.com"
    }],
    "hobby": {
        "id": 1,
        "hname": "xiaohuahua",
        "hdesc": "xiaohuahua@qq.com"
    }
}

Qt_C++++JSON 转结构体代码说明

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "jsontest.h"
#include "x2struct/x2struct.hpp"   // 此库需要下载然后包含到项目中
#include <iostream>
#include <vector>
#include <map>


using namespace std;


// 定义数组嵌套的 json
struct User {
    int id;

    string name;
    string mail;

    User(int i = 0, const string& n = "", const string& m = "") :
        id(i),name(n),mail(m){}
    XTOSTRUCT(O(id, name, mail));

};

// 定义内层嵌套 json
struct Hobby {

    int id;

    string hname;
    string hdesc;

    Hobby(int hi = 0, const string& hn = "", const string& hd = ""):
    id(hi), hname(hn), hdesc(hd){}

    XTOSTRUCT(O(id, hname, hdesc));

};

// 定以外层 json 
struct Group {
    string name;
    int master;
    vector<User> members;

    Hobby hobby;
    XTOSTRUCT(O(name, master, members, hobby));  // 此处不要忘记添加,我在此处犯错了
};



MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);


    Group g;
    g.name = "C 加加";
    g.master = 2020;
    g.members.resize(2);
    g.members[0] = User(1, "xiaohua", "xiaohua@qq.com");
    g.members[1] = User(2, "xiaohe", "xiaohe@qq.com");
    g.hobby = Hobby(1, "xiaohuahua", "xiaohuahua@qq.com");

//    g.hobbyMap.clear();
//    g.hobbyMap["hobby"] = Hobby(1, "xiaohuahua", "xiaohuahua@qq.com");

    string json = x2struct::X::tojson(g);
    cout <<json << endl;

//    Group n;
//    x2struct::X::loadjson(json, n, false);

//    for(auto nitem : n.members)
//    {
//        cout << nitem.id << endl;
//        cout << nitem.name <<endl;
//        cout << nitem.mail <<endl;
//    }



//    xstruct x;

//    // struct <--> string
//    cout<<"======== struct <-----> string ==========="<<endl;
//    X::loadjson("test.json", x, true);  // load json from file, if from string, last parameter give false
//    cout<<X::tojson(x)<<endl<<endl;     // struct to string.

//    // map <--> string
//    cout<<"======== map <-----> string ==========="<<endl;
//    string smap = "{\"a\":1, \"b\":2}";
//    map<string, int> m;
//    X::loadjson(smap, m, false);
//    cout<<X::tojson(m)<<endl<<endl;

//    // vector <--> string
//    cout<<"======== vector <-----> string ==========="<<endl;
//    string svector = "[1, 2, 3]";
//    vector<int> v;
//    X::loadjson(svector, v, false);
//    cout<<X::tojson(v)<<endl<<endl;



}

MainWindow::~MainWindow()
{
    delete ui;
}

Qt_C++++JSON 转结构体最终实现效果

Qt_C++JSON转结构体
posted @ 2020-04-13 15:51  不随。  阅读(41)  评论(0编辑  收藏  举报  来源