#include "widget.h"
#include "ui_widget.h"
#include <QFile>
#include <QJsonValue>
#include <QJsonArray>
#include <QJsonDocument>
#include <QByteArray>
#include <QDebug>
#include <QJsonObject>
#include <QList>
QList<QJsonObject> m_metadataList;
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
/写入JSON文件/
// //定义一个Json数组
// QJsonArray array;
// //向Json数组中追加数据
// array.append(QJsonValue(10));
// array.append(QJsonValue("hello itcast"));
// array.append(QJsonValue(3.1415));
// array.append(QJsonValue("Qt"));
// //转为Json文档
// QJsonDocument doc(array);
//
// QJsonObject metadataObject;
// metadataObject["strUrl"] = "http://47.110.162.166:9001//edata/base/JIAN_HANG_TAI_DA-BASE-WK23_24.zip?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20240611T021248Z&X-Amz-SignedHeaders=host&X-Amz-Expires=604800&X-Amz-Credential=minioroot%2F20240611%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=13a9b4762cba8ca8eeab3dbd38ef7adc6976c0834aa349a430ef35dbe041f8a4";
// metadataObject["filepath"] = "/home/ry/20240517/build-NRECS-Desktop-Debug/bin/ENCData";
// metadataObject["filename"] = "JIAN_HANG_TAI_DA-BASE-WK23_24.zip";
// metadataObject["base"] = false;
// metadataObject["file_type"] = 2;
// metadataObject["totalSize"] = 54151356;
// QJsonArray jsonArray;
// jsonArray.append(metadataObject);
// QJsonDocument doc(jsonArray);
// QFile metadataFile("download_metadata.json");
// if (metadataFile.open(QIODevice::WriteOnly)) {
// qDebug() << "doc.toJson(): " << doc.toJson();
// metadataFile.write(doc.toJson());
// metadataFile.close();
// }
/读取JSON文件///
m_metadataList.clear();
QFile metadataFile("download_metadata.json");
if (metadataFile.open(QIODevice::ReadOnly)) {
QByteArray data = metadataFile.readAll();
metadataFile.close();
QJsonDocument doc = QJsonDocument::fromJson(data);
if (doc.isArray()) {
QJsonArray jsonArray = doc.array();
for (const QJsonValue &value : qAsConst(jsonArray)) {
if (value.isObject()) {
m_metadataList.append(value.toObject());
}
}
}
}
QJsonArray jsonArray;
bool flag = true;
for (const QJsonObject &metadata : m_metadataList) {
qDebug() << metadata["base"].toBool();
}
}
Widget::~Widget()
{
delete ui;
}
QT 读写JSON文件
最新推荐文章于 2024-08-10 19:44:52 发布