本介绍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
注意,预编译相关设置一定要有,否则编译会报错。
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
实现文件
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" ); t.id (&JsonModel::id, "id" ); t.data (&JsonModel::name, "name" , 1 ); t.data (&JsonModel::age, "age" ); } }QX_REGISTER_CPP_EXPORT_DLL (JsonModel2)namespace qx { template <> void register_class (QxClass<JsonModel2> & t) { t.setName ("JsonModel2" ); t.id (&JsonModel2::id, "id" ); t.data (&JsonModel2::name, "name" , 1 ); 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 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" ); 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" }