QJsonDocument 类

QJsonDocument 类提供读写 JSON 文档的办法。 更多...

头: #include <QJsonDocument>
qmake: QT += core
Since: Qt 5.0

该类在 Qt 5.0 引入。

注意: 此类的所有函数 可重入 .

公共类型

enum DataValidation { Validate, BypassValidation }
enum JsonFormat { Indented, Compact }

公共函数

QJsonDocument (QJsonDocument && other )
QJsonDocument (const QJsonDocument & other )
QJsonDocument (const QJsonArray & array )
QJsonDocument (const QJsonObject & object )
QJsonDocument ()
QJsonDocument & operator= (QJsonDocument && other )
QJsonDocument & operator= (const 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 () const
QByteArray toJson (QJsonDocument::JsonFormat format ) const
QVariant toVariant () const
bool operator!= (const QJsonDocument & other ) const
bool operator== (const QJsonDocument & other ) const
const QJsonValue operator[] (const QString & key ) const
const QJsonValue operator[] (QStringView key ) const
const QJsonValue operator[] (QLatin1String key ) const
const QJsonValue operator[] (int i ) const

静态公共成员

QJsonDocument fromJson (const QByteArray & json , QJsonParseError * error = nullptr)
QJsonDocument fromVariant (const QVariant & variant )

详细描述

QJsonDocument is a class that wraps a complete JSON document and can read and write this document both from a UTF-8 encoded text based representation as well as Qt's own binary format.

A JSON document can be converted from its text-based representation to a QJsonDocument using QJsonDocument::fromJson (). toJson () converts it back to text. The parser is very fast and efficient and converts the JSON to the binary representation used by Qt.

Validity of the parsed document can be queried with ! isNull ()

A document can be queried as to whether it contains an array or an object using isArray () 和 isObject (). The array or object contained in the document can be retrieved using array () 或 object () and then read or manipulated.

A document can also be created from a stored binary representation using fromBinaryData() or fromRawData().

另请参阅 在 Qt 中支持 JSON and JSON 保存游戏范例 .

成员类型文档编制

enum QJsonDocument:: DataValidation

This value is used to tell QJsonDocument whether to validate the binary data when converting to a QJsonDocument using fromBinaryData() or fromRawData().

常量 描述
QJsonDocument::Validate 0 Validate the data before using it. This is the default.
QJsonDocument::BypassValidation 1 Bypasses data validation. Only use if you received the data from a trusted place and know it's valid, as using of invalid data can crash the application.

enum QJsonDocument:: JsonFormat

This value defines the format of the JSON byte array produced when converting to a QJsonDocument 使用 toJson ().

常量 描述
QJsonDocument::Indented 0 定义人性化可读输出如下:
    {
        "Array": [
            true,
            999,
            "string"
        ],
        "Key": "Value",
        "null": null
    }
								
QJsonDocument::Compact 1 Defines a compact output as follows:
    {"Array":[true,999,"string"],"Key":"Value","null":null}
								

该枚举在 Qt 5.1 引入或被修改。

成员函数文档编制

QJsonDocument:: QJsonDocument ( QJsonDocument && other )

移动构造 QJsonDocument 从 other .

该函数在 Qt 5.10 引入。

QJsonDocument:: QJsonDocument (const QJsonDocument & other )

创建副本为 other 文档。

QJsonDocument:: QJsonDocument (const QJsonArray & array )

构造 QJsonDocument 从 array .

QJsonDocument:: QJsonDocument (const QJsonObject & object )

创建 QJsonDocument 从 object .

QJsonDocument:: QJsonDocument ()

构造空的无效文档。

QJsonDocument &QJsonDocument:: operator= ( QJsonDocument && other )

移动赋值 other 到此文档。

该函数在 Qt 5.10 引入。

QJsonDocument &QJsonDocument:: operator= (const QJsonDocument & other )

赋值 other 文档到此 QJsonDocument 。返回此对象的引用。

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 从它。

返回有效 (非 null) QJsonDocument 若剖析成功。若它失败,返回文档将为 null,且可选 error 变量将包含有关错误的进一步细节。

另请参阅 toJson (), QJsonParseError ,和 isNull ().

[static] QJsonDocument QJsonDocument:: fromVariant (const QVariant & variant )

创建 QJsonDocument QVariant variant .

variant 包含任何其它类型除了 QVariantMap , QVariantHash , QVariantList or QStringList ,返回的文档无效。

另请参阅 toVariant ().

bool QJsonDocument:: isArray () const

返回 true 若文档包含数组。

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

bool QJsonDocument:: isEmpty () const

返回 true 若文档未包含任何数据。

bool QJsonDocument:: isNull () const

返回 true 若此文档为 null。

null 是透过默认构造函数创建文档。

Documents created from UTF-8 encoded text or the binary format are validated during parsing. If validation fails, the returned document will also be null.

bool QJsonDocument:: isObject () const

返回 true 若文档包含对象。

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

QJsonObject QJsonDocument:: object () const

返回 QJsonObject 包含在文档中。

返回空对象若文档包含数组。

另请参阅 isObject (), array (),和 setObject ().

void QJsonDocument:: setArray (const QJsonArray & array )

设置 array 作为此文档的 main 对象。

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

void QJsonDocument:: setObject (const QJsonObject & object )

设置 object 作为此文档的 main 对象。

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

void QJsonDocument:: swap ( QJsonDocument & other )

Swaps the document other with this. This operation is very fast and never fails.

该函数在 Qt 5.10 引入。

QByteArray QJsonDocument:: toJson () const

转换 QJsonDocument 成缩进的 UTF-8 编码 JSON 文档。

另请参阅 fromJson ().

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

转换 QJsonDocument 成 UTF-8 编码 JSON 文档在提供 format .

该函数在 Qt 5.1 引入。

另请参阅 fromJson () 和 JsonFormat .

QVariant QJsonDocument:: toVariant () const

返回 QVariant 表示 Json 文档。

返回的变体将是 QVariantList 若文档为 QJsonArray QVariantMap 若文档为 QJsonObject .

另请参阅 fromVariant () 和 QJsonValue::toVariant ().

bool QJsonDocument:: operator!= (const QJsonDocument & other ) const

返回 true if other 不等于此文档

bool QJsonDocument:: operator== (const QJsonDocument & other ) const

返回 true other 文档等于此文档。

const QJsonValue QJsonDocument:: operator[] (const QString & key ) const

返回 QJsonValue 表示值为键 key .

相当于调用 object ().value(key).

返回的 QJsonValue is QJsonValue::Undefined 若键不存在,或者若 isObject () 为 false。

该函数在 Qt 5.10 引入。

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

const QJsonValue QJsonDocument:: operator[] ( QStringView key ) const

这是重载函数。

该函数在 Qt 5.14 引入。

const QJsonValue QJsonDocument:: operator[] ( QLatin1String key ) const

这是重载函数。

该函数在 Qt 5.10 引入。

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

返回 QJsonValue representing the value for index i .

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

返回的 QJsonValue is QJsonValue::Undefined ,若 i 超出边界,或者若 isArray () 为 false。

该函数在 Qt 5.10 引入。

另请参阅 QJsonValue , QJsonValue::isUndefined (),和 QJsonArray .