QxOrm入门教程03.02:JSON

本介绍JSON相关操作

工程

创建一个纯C++工程,设置pro

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
include(D:\gsdf\include\QxOrm\QxOrm.pri)

CONFIG += c++17 console
DEFINES += _BUILDING_USER
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

!contains(DEFINES, _QX_NO_PRECOMPILED_HEADER) {
PRECOMPILED_HEADER = precompiled.h
}

INCLUDEPATH += D:\gsdf\include\QxOrm\include

LIBS += -LD:\gsdf\lib -lQxOrmd

HEADERS += \
user.h

SOURCES += \
main.cpp \
user.cpp

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

主要就是添加QxOrm包含文件,设置预编译头文件,添加链接库

源码

预编译

对应的precompiled.h文件

1
2
3
4
5
6
#ifndef PRECOMPILED_H
#define PRECOMPILED_H

#include <QxOrm.h>

#endif // PRECOMPILED_H

注意,预编译相关设置一定要有,否则编译会报错。

JsonModel类

头文件

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
#ifndef JSONMODEL_H
#define JSONMODEL_H

#include "QxCommon/QxMacro.h"
#include "QxOrm.h"
#include "QxRegister/QxRegister.h"
#include "QxPrecompiled.h"
#include "QxOrm_Impl.h"

class JsonModel
{
public:
JsonModel() : id(0) { }
~JsonModel() { }

long id;
QString name;
int age;
};

QX_REGISTER_HPP_EXPORT_DLL(JsonModel, qx::trait::no_base_class_defined, 1)

class JsonModel2
{
public:
JsonModel2() : id(0) { }
~JsonModel2() { }

long id;
QString name;
int age;
JsonModel model;
};
QX_REGISTER_HPP_EXPORT_DLL(JsonModel2, qx::trait::no_base_class_defined, 1)

#endif // JSONMODEL_H

实现文件

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
#include "jsonmodel.h"

QX_REGISTER_CPP_EXPORT_DLL(JsonModel)

namespace qx {
template <> void register_class(QxClass<JsonModel> & t)
{
t.setName("JsonModel");
// 注册 User::id <=> 数据库中的主键
t.id(&JsonModel::id, "id");

// 注册 User::name 属性,使用的 key 是 name,version 是 1。
t.data(&JsonModel::name, "name", 1);

// 注册 User::age 属性,使用的 key 是 age。
t.data(&JsonModel::age, "age");
}
}

QX_REGISTER_CPP_EXPORT_DLL(JsonModel2)

namespace qx {
template <> void register_class(QxClass<JsonModel2> & t)
{
t.setName("JsonModel2");
// 注册 User::id <=> 数据库中的主键
t.id(&JsonModel2::id, "id");

// 注册 User::name 属性,使用的 key 是 name,version 是 1。
t.data(&JsonModel2::name, "name", 1);

// 注册 User::age 属性,使用的 key 是 age。
t.data(&JsonModel2::age, "age");

t.data(&JsonModel2::model, "model");
}
}

主程序文件

其实就是main文件,功能就是填充数据,生成json格式数据。

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
#include "QxOrm_Impl.h"
#include "QxDao/QxSqlDatabase.h"
#include "QxDao/QxDao.h"
#include "QxCommon/QxCache.h"

#include "jsonmodel.h"

int main(int argc, char *argv[])
{
JsonModel json;
json.id = 1;
json.age = 100;
json.name = "gt";

JsonModel2 json2;
json2.id = 2;
json2.age = 200;
json2.name = "gt2";
json2.model = json;
QString jsonStr = qx::serialization::json::to_string(json2);

qDebug()<<jsonStr;

return 0;
}

可以直接保存至文件,但是要和sql的模式一致

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 将容器中的用户导出到 Json 文件中(序列化)
typedef QSharedPointer<JsonModel2> UM2Ptr;
UM2Ptr u1;
u1.reset(new JsonModel2());
u1->id = 2;
u1->name = "gt2";
u1->model.name="gt1";
typedef QVector<UM2Ptr> VectorUser;
VectorUser users;
users.push_back(u1);
qx::serialization::json::to_file(users, "./export_users.json");

// 将 Json 文件中的用户导入至新容器
VectorUser usersJsonTmp;
qx::serialization::json::from_file(usersJsonTmp, "./export_users.json");
qDebug()<<usersJsonTmp[0]->model.name;

运行

编译运行,程序输出为

1
2
3
"{\n    \"age\": 200,\n    \"id\": 2,\n    \"model\": {\n        \"age\": 100,\n
\"id\": 1,\n \"name\": \"gt\"\n },\n \"name\": \"gt2\"\n}\n
"

格式化之后

1
2
3
4
5
6
7
8
9
10
{
"age": 200,
"id": 2,
"model": {
"age": 100,
"id": 1,
"name": "gt"
},
"name": "gt2"
}

QxOrm入门教程03.02:JSON
https://blog.jackeylea.com/qxorm/how-to-access-json-with-qxorm/
作者
JackeyLea
发布于
2025年5月2日
许可协议