这里给出封装的两个函数,用于将结构体st_subject和Json之间进行相互转化:
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>
#include <QJsonDocument>
// 将st_subject结构体转换为Json
QJsonObject st_subjectToJson(const st_subject& subject) {
QJsonObject json;
json["subject_number"] = subject.subject_number;
json["subject_type"] = subject.subject_type;
json["subject_name"] = subject.subject_name;
// 图片信息转换为Base64字符串存储
QByteArray imageData;
QBuffer buffer(&imageData);
buffer.open(QIODevice::WriteOnly);
subject.image.save(&buffer, "PNG"); // 假设图片格式为PNG
QString base64Image = QString::fromLatin1(imageData.toBase64().data());
json["image"] = base64Image;
// 根据题目类型存储对应的答案信息
switch (subject.subject_type) {
case 0: { // True or False
QJsonObject trueFalseAnswerObj;
trueFalseAnswerObj["banswer"] = subject.subject_Type.trueOrfalse.banswer;
trueFalseAnswerObj["banswer_standard"] = subject.subject_Type.trueOrfalse.banswer_standard;
trueFalseAnswerObj["bret"] = subject.subject_Type.trueOrfalse.bret;
json["subject_Type"] = trueFalseAnswerObj;
break;
}
case 1: { // Choose
QJsonObject chooseAnswerObj;
chooseAnswerObj["banswer"] = subject.subject_Type.choose.banswer;
chooseAnswerObj["banswer_standard"] = subject.subject_Type.choose.banswer_standard;
chooseAnswerObj["bret"] = subject.subject_Type.choose.bret;
json["subject_Type"] = chooseAnswerObj;
break;
}
case 2: { // Choose Multiple
QJsonObject chooseMultipleAnswerObj;
QJsonArray banswerArray, banswerStandardArray;
for (int i : subject.subject_Type.choose_multiple.banswer) {
banswerArray.append(i);
}
for (int i : subject.subject_Type.choose_multiple.banswer_standard) {
banswerStandardArray.append(i);
}
chooseMultipleAnswerObj["banswer"] = banswerArray;
chooseMultipleAnswerObj["banswer_standard"] = banswerStandardArray;
chooseMultipleAnswerObj["bret"] = subject.subject_Type.choose_multiple.bret;
json["subject_Type"] = chooseMultipleAnswerObj;
break;
}
}
return json;
}
// 将Json转换为st_subject结构体
st_subject jsonToSt_subject(const QJsonObject& json) {
st_subject subject;
subject.subject_number = json["subject_number"].toInt();
subject.subject_type = json["subject_type"].toInt();
subject.subject_name = json["subject_name"].toString();
// 从Base64字符串还原图片信息
QString base64Image = json["image"].toString();
QByteArray imageData = QByteArray::fromBase64(base64Image.toLatin1());
QImage image;
image.loadFromData(imageData); // 假设图片格式为PNG
subject.image = image;
// 根据题目类型提取对应的答案信息
switch (subject.subject_type) {
case 0: { // True or False
QJsonObject trueFalseAnswerObj = json["subject_Type"].toObject();
subject.subject_Type.trueOrfalse.banswer =
trueFalseAnswerObj["banswer"].toBool();
subject.subject_Type.trueOrfalse.banswer_standard =
trueFalseAnswerObj["banswer_standard"].toBool();
subject.subject_Type.trueOrfalse.bret =
trueFalseAnswerObj["bret"].toBool();
break;
}
case 1: { // Choose
QJsonObject chooseAnswerObj = json["subject_Type"].toObject();
subject.subject_Type.choose.banswer =
chooseAnswerObj["banswer"].toInt();
subject.subject_Type.choose.banswer_standard =
chooseAnswerObj["banswer_standard"].toInt();
subject.subject_Type.choose.bret =
chooseAnswerObj["bret"].toBool();
break;
}
case 2: { // Choose Multiple
QJsonObject chooseMultipleAnswerObj = json["subject_Type"].toObject();
QJsonArray banswerArray = chooseMultipleAnswerObj["banswer"].toArray();
for (const QJsonValue& value : banswerArray) {
subject.subject_Type.choose_multiple.banswer.append(value.toInt());
}
QJsonArray banswerStandardArray = chooseMultipleAnswerObj["banswer_standard"].toArray();
for (const QJsonValue& value : banswerStandardArray) {
subject.subject_Type.choose_multiple.banswer_standard.append(value.toInt());
}
subject.subject_Type.choose_multiple.bret =
chooseMultipleAnswerObj["bret"].toBool();
break;
}
}
return subject;
}
这样就可以使用这两个函数来进行结构体st_subject和Json之间的相互转化了。希望对你有帮助!
内容由零声教学AI助手提供,问题来源于学员提问