if
(
!
loadFile
.
open
(
QIODevice
:
:
ReadOnly
)
)
qDebug
(
)
<<
"could't open projects json"
;
return
;
QByteArray allData
=
loadFile
.
readAll
(
)
;
loadFile
.
close
(
)
;
QJsonParseError jsonError
;
QJsonDocument
jsonDoc
(
QJsonDocument
:
:
fromJson
(
allData
,
&
jsonError
)
)
;
if
(
jsonError
.
error
!=
QJsonParseError
:
:
NoError
)
qDebug
(
)
<<
"json error!"
<<
jsonError
.
errorString
(
)
;
return
;
QJsonObject rootObj
=
jsonDoc
.
object
(
)
;
QStringList keys
=
rootObj
.
keys
(
)
;
for
(
int
i
=
0
;
i
<
keys
.
size
(
)
;
i
++
)
qDebug
(
)
<<
"key"
<<
i
<<
" is:"
<<
keys
.
at
(
i
)
;
if
(
rootObj
.
contains
(
"first fruit"
)
)
QJsonObject subObj
=
rootObj
.
value
(
"first fruit"
)
.
toObject
(
)
;
qDebug
(
)
<<
"describe is:"
<<
subObj
[
"describe"
]
.
toString
(
)
;
qDebug
(
)
<<
"icon is:"
<<
subObj
[
"icon"
]
.
toString
(
)
;
qDebug
(
)
<<
"name is:"
<<
subObj
[
"name"
]
.
toString
(
)
;
if
(
rootObj
.
contains
(
"second fruit"
)
)
QJsonObject subObj
=
rootObj
.
value
(
"second fruit"
)
.
toObject
(
)
;
qDebug
(
)
<<
"describe is:"
<<
subObj
.
value
(
"describe"
)
.
toString
(
)
;
qDebug
(
)
<<
"icon is:"
<<
subObj
.
value
(
"icon"
)
.
toString
(
)
;
qDebug
(
)
<<
"name is:"
<<
subObj
.
value
(
"name"
)
.
toString
(
)
;
if
(
rootObj
.
contains
(
"three fruit array"
)
)
QJsonArray subArray
=
rootObj
.
value
(
"three fruit array"
)
.
toArray
(
)
;
for
(
int
i
=
0
;
i
<
subArray
.
size
(
)
;
i
++
)
qDebug
(
)
<<
i
<<
" value is:"
<<
subArray
.
at
(
i
)
.
toString
(
)
;
需要包含的头文件:
#include <QJsonDocument>
#include <QJsonParseError>
#include <QFile>
#include <QJsonObject>
#include <QDebug>
#include <QJsonArray>
运行结果:
key 0 is: "first fruit"
key 1 is: "second fruit"
key 2 is: "three fruit array"
describe is: "an apple"
icon is: "appleIcon"
name is: "apple"
describe is: "an orange"
icon is: "orangeIcon"
name is: "orange"
0 value is: "eat 0"
1 value is: "eat 1"
2 value is: "eat 2"
3 value is: "eat 3"
4 value is: "eat 4"
Qt官方文档JSON Save Game Example
也可以看:JSON实战之savegame(Qt Example)
每次读取之前,都要判断
void Character::read(const QJsonObject &json)
if (json.contains("name") && json["name"].isString())
mName = json["name"].toString();
if (json.contains("level") && json["level"].isDouble())
mLevel = json["level"].toInt();
if (json.contains("classType") && json["classType"].isDouble())
mClassType = ClassType(json["classType"].toInt());
先用contains判断是否存在,再用isString判断是能转换。
bool isArray() const
bool isBool() const
bool isDouble() const
bool isNull() const
bool isObject() const
bool isString() const
bool isUndefined() const
修改后的代码为:
QFile loadFile("D:\\1.json");
if(!loadFile.open(QIODevice::ReadOnly))
qDebug() << "could't open projects json";
return;
QByteArray allData = loadFile.readAll();
loadFile.close();
QJsonParseError json_error;
QJsonDocument jsonDoc(QJsonDocument::fromJson(allData, &json_error));
if(json_error.error != QJsonParseError::NoError)
qDebug() << "json error!";
return;
QJsonObject rootObj = jsonDoc.object();
QStringList keys = rootObj.keys();
for(int i = 0; i < keys.size(); i++)
qDebug() << "key" << i << " is:" << keys.at(i);
if(rootObj.contains("first fruit") && rootObj["first fruit"].isObject())
QJsonObject subObj = rootObj["first fruit"].toObject();
if (subObj.contains("describe") && subObj["describe"].isString())
qDebug() << "describe is:" << subObj["describe"].toString();
if (subObj.contains("icon") && subObj["icon"].isString())
qDebug() << "icon is:" << subObj["icon"].toString();
if (subObj.contains("name") && subObj["name"].isString())
qDebug() << "name is:" << subObj["name"].toString();
if(rootObj.contains("second fruit") && rootObj["second fruit"].isObject())
QJsonObject subObj = rootObj["second fruit"].toObject();
if (subObj.contains("describe") && subObj["describe"].isString())
qDebug() << "describe is:" << subObj["describe"].toString();
if (subObj.contains("icon") && subObj["icon"].isString())
qDebug() << "icon is:" << subObj["icon"].toString();
if (subObj.contains("name") && subObj["name"].isString())
qDebug() << "name is:" << subObj["name"].toString();
if(rootObj.contains("three fruit array") && rootObj["three fruit array"].isArray())
QJsonArray subArray = rootObj.value("three fruit array").toArray();
for(int i = 0; i< subArray.size(); i++)
if (subArray[i].isString())
qDebug() << i<<" value is:" << subArray.at(i).toString();
Json文件是这样:{ &quot;first fruit&quot;: { &quot;describe&quot;:&quot;an apple&quot;, &quot;icon&quot;:&quot;appleIcon&quot;, &quot;name&quot;:&quot;apple&quot; },
Qt5 中包含了处理 JSON 的类,均以 QJson 开头(例如:QJsonDocument、QJsonArray、QJsonObject),在 QtCore 模块中,不需要额外引入其它模块。
代码如下:
#include <QCoreApplication>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonPars
QJsonDocument类提供了一种读取和写入JSON文档的方法。
QJsonDocument是一个包装完整JSON文档的类,它既可以从基于UTF-8编码的文本表示法,也可以从Qt自己的二进制格式中读取和写入这个文档。
一个JSON文档可以使用QJsonDocument::fromJson()从基于文本的表示方式转换为QJsonDocument,toJson()将其转换回文本。该解析器非常快速和高效
一、读出字符串
QFile json_file(filepath);
json_file.open(QIODevice::ReadOnly | QIODevice::Text);
if(json_file.open(QIODevice::ReadOnly | QIODevice::Text))
json是一种轻便量级的数据交换格式,具有良好的可读和便于快速编写的特性。可以在不同的平台间进行数据交换,都是由key:value的格式组成。下面是qt对json格式数据的基本操作
QJsonArray 封装 JSON 数组
QJsonDocument 读写 JSON 文档
QJsonObject ...
在 Qt 中,可以使用 QFileDialog 类打开文件对话框来选择要读取的 JSON 文件,然后使用 QJsonDocument 类解析 JSON 数据。
以下是一个简单的示例:
```cpp
#include <QApplication>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QFile>
#include <QFileDialog>
int main(int argc, char *argv[])
QApplication a(argc, argv);
// 打开文件对话框选择 JSON 文件
QString fileName = QFileDialog::getOpenFileName(nullptr, "Open JSON File", QString(), "JSON Files (*.json)");
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
qDebug() << "Failed to open JSON file: " << file.errorString();
return 1;
// 解析 JSON 数据
QByteArray jsonData = file.readAll();
QJsonDocument jsonDoc(QJsonDocument::fromJson(jsonData));
QJsonObject jsonObj = jsonDoc.object();
// 读取 JSON 数据
QString name = jsonObj["name"].toString();
int age = jsonObj["age"].toInt();
QJsonArray hobbiesArray = jsonObj["hobbies"].toArray();
QStringList hobbies;
for (const auto &hobby : hobbiesArray) {
hobbies.append(hobby.toString());
qDebug() << "Name: " << name;
qDebug() << "Age: " << age;
qDebug() << "Hobbies: " << hobbies.join(", ");
return a.exec();
在上面的示例中,我们首先使用 `QFileDialog::getOpenFileName` 函数打开文件对话框,让用户选择要读取的 JSON 文件。然后,我们使用 `QFile` 类打开文件并读取 JSON 数据。接下来,使用 `QJsonDocument` 类解析 JSON 数据并将其转换为 `QJsonObject` 对象。最后,我们可以使用 `QJsonObject` 对象读取 JSON 数据。
请注意,上面的示例假设 JSON 文件包含以下数据:
```json
"name": "Alice",
"age": 25,
"hobbies": [
"reading",
"swimming",
"traveling"