Qt5
提供了一个非常方便的JSON解析器,使得在C++中处理JSON数据变得非常简单。本文将详细介绍如何在Qt5中创建、读取和写入JSON文件。
读取JSON文件的示例
假设我们有一个名为test.json
的JSON文件,内容如下:
{
"appDesc": {
"description": "SomeDescription",
"message": "SomeMessage"
},
"appName": {
"description": "Home",
"message": "Welcome",
"imp": ["awesome", "best", "good"]
}
}
我们可以通过以下代码读取这个JSON文件:
void readJson() {
QString val;
QFile file;
file.setFileName("test.json");
file.open(QIODevice::ReadOnly | QIODevice::Text);
val = file.readAll();
file.close();
qWarning() << val;
QJsonDocument d = QJsonDocument::fromJson(val.toUtf8());
QJsonObject sett2 = d.object();
QJsonValue value = sett2.value(QString("appName"));
qWarning() << value;
QJsonObject item = value.toObject();
qWarning() << tr("QJsonObject of description: ") << item;
// 获取字符串值并转换为字符串
qWarning() << tr("QJsonObject[appName] of description: ") << item["description"];
QJsonValue subobj = item["description"];
qWarning() << subobj.toString();
// 获取数组值并转换为字符串
qWarning() << tr("QJsonObject[appName] of value: ") << item["imp"];
QJsonArray test = item["imp"].toArray();
qWarning() << test[1].toString();
}
执行上述代码后,控制台输出如下:
QJsonValue(object, QJsonObject({"description": "Home","imp": ["awesome","best","good"],"message": "Welcome"}) )
"QJsonObject of description: " QJsonObject({"description": "Home","imp": ["awesome","best","good"],"message": "Welcome"})
"QJsonObject[appName] of description: " QJsonValue(string, "Home")
"Home"
"QJsonObject[appName] of value: " QJsonValue(array, QJsonArray(["awesome","best","good"]))
"best"
解析JSON字符串的示例
我们也可以将一个JSON字符串解析为JSON对象:
void readJsonFromString() {
QString jsonString = R"(
{
"appDesc": {
"description": "SomeDescription",
"message": "SomeMessage"
},
"appName": {
"description": "Home",
"message": "Welcome",
"imp": ["awesome", "best", "good"]
}
})";
QJsonDocument d = QJsonDocument::fromJson(jsonString.toUtf8());
QJsonObject sett2 = d.object();
QJsonValue value = sett2.value(QString("appName"));
qWarning() << value;
QJsonObject item = value.toObject();
qWarning() << tr("QJsonObject of description: ") << item;
qWarning() << tr("QJsonObject[appName] of description: ") << item["description"];
QJsonValue subobj = item["description"];
qWarning() << subobj.toString();
qWarning() << tr("QJsonObject[appName] of value: ") << item["imp"];
QJsonArray test = item["imp"].toArray();
qWarning() << test[1].toString();
}
创建和写入JSON文件的示例
下面的代码展示了如何创建一个带有结构的JSON文件:
void createJson() {
QFile file(QDir::homePath() + "/1.json");
if(!file.open(QIODevice::ReadWrite)) {
qDebug() << "File open error";
} else {
qDebug() << "File open!";
}
// 清空文件内容
file.resize(0);
// 创建JSON数组,并写入到文件
QJsonArray jsonArray;
for(int i = 0; i < 10; i++) {
QJsonObject jsonObject;
jsonObject.insert("Date", QDateTime::currentDateTime().toString());
jsonObject.insert("Band", "20");
QJsonObject jsonSenderLatObject;
jsonSenderLatObject.insert("Lat", 13);
jsonSenderLatObject.insert("Lon", 122);
jsonSenderLatObject.insert("Sender", "DX0HQ");
QJsonObject jsonReceiverLatObject;
jsonReceiverLatObject.insert("Lat", 36.400001525878906);
jsonReceiverLatObject.insert("Lon", 138.3800048828125);
jsonReceiverLatObject.insert("Receiver", "8N3HQ");
jsonObject.insert("Receiver", jsonReceiverLatObject);
jsonObject.insert("Sender", jsonSenderLatObject);
jsonArray.append(jsonObject);
}
QJsonObject finalObject;
finalObject.insert("number", jsonArray.size());
jsonArray.append(finalObject);
QJsonDocument jsonDoc;
jsonDoc.setArray(jsonArray);
file.write(jsonDoc.toJson());
file.close();
qDebug() << "Write to file";
}
输出的JSON文件内容如下:
[
{
"Band": "20",
"Date": "Sat Jul 10 12:00:00 2021",
"Receiver": {
"Lat": 36.400001525878906,
"Lon": 138.3800048828125,
"Receiver": "8N3HQ"
},
"Sender": {
"Lat": 13,
"Lon": 122,
"Sender": "DX0HQ"
}
},
// 其他条目省略
{
"number": 10
}
]