内容表

上一话题

QJsonArray

下一话题

QJsonParseError

QJsonDocument

QJsonDocument class provides a way to read and write JSON documents. 更多

Inheritance diagram of PySide2.QtCore.QJsonDocument

概要

函数

静态函数

详细描述

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 使用 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() and isObject() . The array or object contained in the document can be retrieved using array() or object() and then read or manipulated.

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

另请参阅

在 Qt 中支持 JSON JSON 保存游戏范例

class QJsonDocument

QJsonDocument(array)

QJsonDocument(other)

QJsonDocument(object)

param array

QJsonArray

param other

QJsonDocument

param object

QJsonObject

构造空无效文档。

PySide2.QtCore.QJsonDocument. DataValidation

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

常量

描述

QJsonDocument.Validate

Validate the data before using it. This is the default.

QJsonDocument.BypassValidation

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.

PySide2.QtCore.QJsonDocument. JsonFormat

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

常量

描述

QJsonDocument.Indented

Defines human readable output as follows:

{
    "Array": [
        true,
        999,
        "string"
    ],
    "Key": "Value",
    "null": null
}
															

QJsonDocument.Compact

Defines a compact output as follows:

{"Array":[true,999,"string"],"Key":"Value","null":null}
															
PySide2.QtCore.QJsonDocument. array ( )
返回类型

QJsonArray

返回 QJsonArray 包含在文档中。

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

static PySide2.QtCore.QJsonDocument. fromBinaryData ( data [ , validation=Validate ] )
参数
返回类型

QJsonDocument

注意

此函数被弃用。

创建 QJsonDocument from data .

validation decides whether the data is checked for validity before being used. By default the data is validated. If the data is not valid, the method returns a null document.

注意

Deprecated in Qt 5.15. The binary JSON encoding is only retained for backwards compatibility. It is undocumented and restrictive in the maximum size of JSON documents that can be encoded. Qt JSON types can be converted to Qt CBOR types, which can in turn be serialized into the CBOR binary format and vice versa. The CBOR format is a well-defined and less restrictive binary representation for a superset of JSON.

static PySide2.QtCore.QJsonDocument. fromJson ( json [ , error=None ] )
参数
返回类型

QJsonDocument

剖析 json 作为 UTF-8 编码 JSON 文档,并创建 QJsonDocument 从它。

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

static PySide2.QtCore.QJsonDocument. fromRawData ( data , size [ , validation=Validate ] )
参数
返回类型

QJsonDocument

注意

此函数被弃用。

创建 QJsonDocument that uses the first size 字节来自 data . It assumes data contains a binary encoded JSON document. The created document does not take ownership of data . The data is copied into a different data structure, and the original data can be deleted or modified afterwards.

data has to be aligned to a 4 byte boundary.

validation decides whether the data is checked for validity before being used. By default the data is validated. If the data is not valid, the method returns a null document.

返回 QJsonDocument representing the data.

注意

Deprecated in Qt 5.15. The binary JSON encoding is only retained for backwards compatibility. It is undocumented and restrictive in the maximum size of JSON documents that can be encoded. Qt JSON types can be converted to Qt CBOR types, which can in turn be serialized into the CBOR binary format and vice versa. The CBOR format is a well-defined and less restrictive binary representation for a superset of JSON.

注意

Before Qt 5.15, the caller had to guarantee that data would not be deleted or modified as long as any QJsonDocument , QJsonObject or QJsonArray still referenced the data. From Qt 5.15 on, this is not necessary anymore.

static PySide2.QtCore.QJsonDocument. fromVariant ( variant )
参数

variant – object

返回类型

QJsonDocument

创建 QJsonDocument QVariant variant .

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

另请参阅

toVariant()

PySide2.QtCore.QJsonDocument. isArray ( )
返回类型

bool

返回 true 若文档包含数组。

PySide2.QtCore.QJsonDocument. isEmpty ( )
返回类型

bool

返回 true if the document doesn’t contain any data.

PySide2.QtCore.QJsonDocument. isNull ( )
返回类型

bool

返回 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.

PySide2.QtCore.QJsonDocument. isObject ( )
返回类型

bool

返回 true 若文档包含对象。

PySide2.QtCore.QJsonDocument. object ( )
返回类型

QJsonObject

返回 QJsonObject 包含在文档中。

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

PySide2.QtCore.QJsonDocument. __ne__ ( other )
参数

other QJsonDocument

返回类型

bool

返回 true if other 不等于此文档

PySide2.QtCore.QJsonDocument. __eq__ ( other )
参数

other QJsonDocument

返回类型

bool

返回 true other document is equal to this document.

PySide2.QtCore.QJsonDocument.operator[](key)
参数

key – unicode

返回类型

QJsonValue

PySide2.QtCore.QJsonDocument.operator[](i)
参数

i int

返回类型

QJsonValue

返回 QJsonValue representing the value for index i .

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

返回的 QJsonValue is Undefined , if i is out of bounds, or if isArray() is false.

PySide2.QtCore.QJsonDocument. rawData ( size )
参数

size int

返回类型

str

注意

此函数被弃用。

Returns the raw binary representation of the data size will contain the size of the returned data.

This method is useful to e.g. stream the JSON document in its binary form to a file.

注意

Deprecated in Qt 5.15. The binary JSON encoding is only retained for backwards compatibility. It is undocumented and restrictive in the maximum size of JSON documents that can be encoded. Qt JSON types can be converted to Qt CBOR types, which can in turn be serialized into the CBOR binary format and vice versa. The CBOR format is a well-defined and less restrictive binary representation for a superset of JSON.

另请参阅

QCborValue

PySide2.QtCore.QJsonDocument. setArray ( array )
参数

array QJsonArray

array 作为此文档的 main 对象。

PySide2.QtCore.QJsonDocument. setObject ( object )
参数

object QJsonObject

object 作为此文档的 main 对象。

PySide2.QtCore.QJsonDocument. swap ( other )
参数

other QJsonDocument

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

PySide2.QtCore.QJsonDocument. toBinaryData ( )
返回类型

QByteArray

注意

此函数被弃用。

Returns a binary representation of the document.

The binary representation is also the native format used internally in Qt, and is very efficient and fast to convert to and from.

The binary format can be stored on disk and interchanged with other applications or computers. fromBinaryData() can be used to convert it back into a JSON document.

注意

Deprecated in Qt 5.15. The binary JSON encoding is only retained for backwards compatibility. It is undocumented and restrictive in the maximum size of JSON documents that can be encoded. Qt JSON types can be converted to Qt CBOR types, which can in turn be serialized into the CBOR binary format and vice versa. The CBOR format is a well-defined and less restrictive binary representation for a superset of JSON.

PySide2.QtCore.QJsonDocument. toJson ( )
返回类型

QByteArray

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

另请参阅

fromJson()

PySide2.QtCore.QJsonDocument. toJson ( format )
参数

format JsonFormat

返回类型

QByteArray

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

另请参阅

fromJson() JsonFormat

PySide2.QtCore.QJsonDocument. toVariant ( )
返回类型

object

返回 QVariant 表示 Json 文档。

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