QJsonDocument Class
QJsonDocument 类提供了一种读写 JSON 文档的方法。更多内容...
| 标题: | #include <QJsonDocument> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
| qmake: | QT += core |
- 所有成员的列表,包括继承的成员
- QJsonDocument 属于Qt 中的 JSON 支持以及隐式共享类。
注意:该类中的所有函数均为可重入函数。
QJsonDocument 比较
| 类别 | 可比较类型 |
|---|---|
| 相等性 | QJsonDocument |
公共类型
| enum | JsonFormat { Indented, Compact } |
公共函数
| QJsonDocument() | |
(since 6.12) | QJsonDocument(QJsonArray &&array) |
(since 6.12) | QJsonDocument(QJsonObject &&object) |
| QJsonDocument(const QJsonArray &array) | |
| QJsonDocument(const QJsonObject &object) | |
| QJsonDocument(const QJsonDocument &other) | |
| QJsonDocument(QJsonDocument &&other) | |
| ~QJsonDocument() | |
| QJsonArray | array() const |
| bool | isArray() const |
| bool | isEmpty() const |
| bool | isNull() const |
| bool | isObject() const |
| QJsonObject | object() const |
| void | setArray(const QJsonArray &array) |
| void | setObject(const QJsonObject &object) |
| void | swap(QJsonDocument &other) |
| QByteArray | toJson(QJsonDocument::JsonFormat format = JsonFormat::Indented) const |
| QVariant | toVariant() const |
| QJsonDocument & | operator=(QJsonDocument &&other) |
| QJsonDocument & | operator=(const QJsonDocument &other) |
| const QJsonValue | operator[](const QString &key) const |
| const QJsonValue | operator[](qsizetype i) const |
| const QJsonValue | operator[](QLatin1StringView key) const |
| const QJsonValue | operator[](QStringView key) const |
静态公共成员
| QJsonDocument | fromJson(const QByteArray &json, QJsonParseError *error = nullptr) |
| QJsonDocument | fromVariant(const QVariant &variant) |
相关的非成员
| bool | operator!=(const QJsonDocument &lhs, const QJsonDocument &rhs) |
| bool | operator==(const QJsonDocument &lhs, const QJsonDocument &rhs) |
详细说明
QJsonDocument 是一个封装完整 JSON 文档的类,能够从 UTF-8 编码的文本表示形式中读取该文档,并将该文档写入该表示形式。
可以通过调用 `QJsonDocument::fromJson()` 将 JSON 文档从其文本表示形式转换为 `QJsonDocument`。`toJson()` 可将其转换回文本。该解析器速度极快且高效,能将 JSON 转换为 Qt 所使用的二进制表示形式。
可通过 !isNull() 查询解析后文档的有效性
可通过isArray() 和isObject() 查询文档是否包含数组或对象。文档中包含的数组或对象可通过array() 或object() 获取,随后进行读取或操作。
另请参阅 Qt 中的 JSON 支持以及游戏的保存与加载。
成员类型文档
enum QJsonDocument::JsonFormat
该值定义了使用 `toJson()` 方法转换为 `QJsonDocument ` 时生成的 JSON 字节数组的格式。
| 常量 | 值 | 描述 |
|---|---|---|
QJsonDocument::Indented | 0 | 定义如下所示的人机可读输出: |
QJsonDocument::Compact | 1 | 定义紧凑输出如下: |
成员函数文档
QJsonDocument::QJsonDocument()
构建一个空的且无效的文档。
[explicit, since 6.12] QJsonDocument::QJsonDocument(QJsonArray &&array)
根据array 构建一个QJsonDocument。
该函数在 Qt 6.12 中引入。
[explicit, since 6.12] QJsonDocument::QJsonDocument(QJsonObject &&object)
根据object 创建一个QJsonDocument。
该函数在 Qt 6.12 中引入。
[explicit] QJsonDocument::QJsonDocument(const QJsonArray &array)
根据array 构建一个QJsonDocument。
[explicit] QJsonDocument::QJsonDocument(const QJsonObject &object)
根据object 创建一个QJsonDocument。
QJsonDocument::QJsonDocument(const QJsonDocument &other)
创建other 文档的副本。
[noexcept] QJsonDocument::QJsonDocument(QJsonDocument &&other)
从 `other` 动态构造一个 `QJsonDocument`。
[noexcept] QJsonDocument::~QJsonDocument()
删除文档。
通过 fromRawData 生成的二进制数据集不会被释放。
QJsonArray QJsonDocument::array() const
返回文档中包含的QJsonArray 。
如果文档中包含一个对象,则返回一个空数组。
另请参阅 isArray()、object() 和setArray()。
[static] QJsonDocument QJsonDocument::fromJson(const QByteArray &json, QJsonParseError *error = nullptr)
将 `json ` 作为 UTF-8 编码的 JSON 文档进行解析,并据此创建一个 `QJsonDocument `。
如果解析成功,则返回一个有效的(非空)QJsonDocument 。如果解析失败,返回的文档将为空,且可选的error 变量将包含有关错误的详细信息。
另请参阅 toJson()、QJsonParseError 以及isNull()。
[static] QJsonDocument QJsonDocument::fromVariant(const QVariant &variant)
根据QVariant variant 创建一个QJsonDocument 。
如果 `variant ` 包含除 `QVariantMap`、`QVariantHash`、`QVariantList ` 或 `QStringList` 以外的任何其他类型,则返回的文档将无效。
另请参阅 toVariant()。
bool QJsonDocument::isArray() const
如果文档包含一个数组,则返回true 。
bool QJsonDocument::isEmpty() const
如果文档中不包含任何数据,则返回true 。
bool QJsonDocument::isNull() const
如果该文档为空,则返回true 。
空文档是指通过默认构造函数创建的文档。
由 UTF-8 编码文本或二进制格式创建的文档将在解析过程中进行验证。如果验证失败,返回的文档也将为 null。
bool QJsonDocument::isObject() const
如果文档中包含一个对象,则返回true 。
QJsonObject QJsonDocument::object() const
返回文档中包含的QJsonObject 。
如果文档包含一个数组,则返回一个空对象。
另请参阅 isObject()、array() 和setObject()。
void QJsonDocument::setArray(const QJsonArray &array)
将array 设置为本文档的主对象。
void QJsonDocument::setObject(const QJsonObject &object)
将object 设置为本文档的主对象。
[noexcept] void QJsonDocument::swap(QJsonDocument &other)
将此文档与other 互换。此操作非常快,且从不会失败。
QByteArray QJsonDocument::toJson(QJsonDocument::JsonFormat format = JsonFormat::Indented) const
将QJsonDocument 转换为UTF-8编码的JSON文档,并将其写入指定的format 中。
另请参阅 fromJson() 和JsonFormat 。
QVariant QJsonDocument::toVariant() const
返回一个表示 JSON 文档的 `QVariant `。
如果文档是QJsonArray ,则返回的变体为QVariantList ;如果文档是QJsonObject ,则返回的变体为QVariantMap 。
另请参阅 fromVariant() 和QJsonValue::toVariant()。
[noexcept] QJsonDocument &QJsonDocument::operator=(QJsonDocument &&other)
将other 移动并分配到此文档中。
QJsonDocument &QJsonDocument::operator=(const QJsonDocument &other)
将other 文档分配给此QJsonDocument 。返回对此对象的引用。
const QJsonValue QJsonDocument::operator[](const QString &key) const
返回一个表示键key 的值的QJsonValue 对象。
等同于调用object().value(key)。
如果该键不存在,或者isObject() 返回 false,则返回的QJsonValue 为QJsonValue::Undefined 。
另请参阅 QJsonValue 、QJsonValue::isUndefined() 和QJsonObject 。
const QJsonValue QJsonDocument::operator[](qsizetype i) const
返回一个表示索引i 所对应值的 `QJsonValue `。
等同于调用array().at(i)。
若i 超出范围,或isArray()返回false,则返回的QJsonValue 即为QJsonValue::Undefined 。
另请参阅 QJsonValue 、QJsonValue::isUndefined() 和QJsonArray 。
const QJsonValue QJsonDocument::operator[](QLatin1StringView key) const
这是一个重载函数。
const QJsonValue QJsonDocument::operator[](QStringView key) const
这是一个重载函数。
相关的非成员
[noexcept] bool operator!=(const QJsonDocument &lhs, const QJsonDocument &rhs)
如果文档lhs 与文档rhs 不相等,则返回true ;否则返回false 。
[noexcept] bool operator==(const QJsonDocument &lhs, const QJsonDocument &rhs)
如果文档lhs 与文档rhs 相等,则返回true ;否则返回false 。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.