QJsonDocument Class

QJsonDocument 类提供了一种读写 JSON 文档的方法。更多

头文件: #include <QJsonDocument>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

该类可等价比较

注意:该类中的所有函数都是可重入的

公共类型

enum JsonFormat { Indented, Compact }

公共函数

QJsonDocument()
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 编码的文本表示读取该文档,也可以将该文档写入基于 UTF-8 编码的文本表示。

可以使用QJsonDocument::fromJson() 将 JSON 文档从基于文本的表示形式转换为 QJsonDocument。toJson() 将其转换回文本。该解析器非常快速高效,能将 JSON 转换为 Qt 使用的二进制表示法。

解析后文档的有效性可通过 !isNull() 查询。

使用isArray() 和isObject() 可以查询文档是否包含数组或对象。文档中包含的数组或对象可以使用array() 或object() 进行检索,然后进行读取或操作。

另请参阅 Qt 中的 JSON 支持以及保存和加载游戏

成员类型文档

enum QJsonDocument::JsonFormat

该值定义了使用toJson() 转换为QJsonDocument 时产生的 JSON 字节数组的格式。

常量说明
QJsonDocument::Indented0定义人可读输出如下:
    {
        "Array": [
            true,
            999,
            "string"
        ],
        "Key": "Value",
        "null": null
    }
QJsonDocument::Compact1定义紧凑型输出如下
    {"Array":[true,999,"string"],"Key":"Value","null":null}

成员函数文档

QJsonDocument::QJsonDocument()

构造一个空的无效文档。

[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)

Move-从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,QVariantListQStringList 以外的任何其他类型,则返回的文档无效。

另请参阅 toVariant() 。

bool QJsonDocument::isArray() const

如果文档包含数组,则返回true

另请参阅 array() 和isObject()。

bool QJsonDocument::isEmpty() const

如果文档不包含任何数据,则返回true

bool QJsonDocument::isNull() const

如果该文档为空,则返回true

空文档是通过默认构造函数创建的文档。

由 UTF-8 编码文本或二进制格式创建的文档会在解析过程中进行验证。如果验证失败,返回的文档也将为空。

bool QJsonDocument::isObject() const

如果文档包含对象,则返回true

另请参阅 object() 和isArray()。

QJsonObject QJsonDocument::object() const

返回文档中包含的QJsonObject

如果文档中包含数组,则返回空对象。

另请参阅 isObject()、array() 和setObject()。

void QJsonDocument::setArray(const QJsonArray &array)

array 设置为本文件的主要对象。

另请参阅 setObject() 和array()。

void QJsonDocument::setObject(const QJsonObject &object)

object 设置为本文件的主要对象。

另请参阅 setArray() 和object()。

[noexcept] void QJsonDocument::swap(QJsonDocument &other)

将此文件与other 互换。这一操作非常快速,从未出现过故障。

QByteArray QJsonDocument::toJson(QJsonDocument::JsonFormat format = JsonFormat::Indented) const

在提供的format 中将QJsonDocument 转换为 UTF-8 编码的 JSON 文档。

另请参阅 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,则返回的QJsonValueQJsonValue::Undefined

另请参阅 QJsonValue,QJsonValue::isUndefined() 和QJsonObject

const QJsonValue QJsonDocument::operator[](qsizetype i) const

返回代表索引i 值的QJsonValue

相当于调用array().at(i) 。

如果i 越界,或isArray() 为 false,则返回的QJsonValueQJsonValue::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

© 2025 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.