auto convert struct to qjsonobject
To convert a struct to a QJsonObject in C++, you can follow these steps:
- Include the necessary headers:
#include <QJsonObject>
#include <QJsonDocument>
- Define your struct, for example:
struct MyStruct {
int id;
QString name;
double value;
};
- Create a function to convert the struct to a QJsonObject:
QJsonObject structToJsonObject(const MyStruct& myStruct) {
QJsonObject jsonObject;
jsonObject["id"] = myStruct.id;
jsonObject["name"] = myStruct.name;
jsonObject["value"] = myStruct.value;
return jsonObject;
}
- Use the function to convert your struct to a QJsonObject:
MyStruct myStruct;
myStruct.id = 1;
myStruct.name = "Example";
myStruct.value = 3.14;
QJsonObject jsonObject = structToJsonObject(myStruct);
Now, jsonObject will contain the converted struct as a QJsonObject
原文地址: http://www.cveoy.top/t/topic/iitG 著作权归作者所有。请勿转载和采集!