QJsonDocumentclass provides a way to read and write JSON documents. 更多 …
def
__eq__
(other)
def
__ne__
(other)
def
array
()
def
isArray
()
def
isEmpty
()
def
isNull
()
def
isObject
()
def
object
()
def
operator[]
(i)
def
operator[]
(key)
def
rawData
(size)
def
setArray
(array)
def
setObject
(object)
def
swap
(other)
def
toBinaryData
()
def
toJson
()
def
toJson
(format)
def
toVariant
()
def
fromBinaryData
(data[, validation=Validate])
def
fromJson
(json[, error=None])
def
fromRawData
(data, size[, validation=Validate])
def
fromVariant
(variant)
QJsonDocumentis 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()andisObject(). The array or object contained in the document can be retrieved usingarray()orobject()and then read or manipulated.A document can also be created from a stored binary representation using
fromBinaryData()orfromRawData().另请参阅
在 Qt 中支持 JSON JSON 保存游戏范例
QJsonDocument
¶
QJsonDocument(array)
QJsonDocument(other)
QJsonDocument(object)
- param array
- param other
- 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
包含在文档中。
返回空数组若文档包含对象。
另请参阅
PySide2.QtCore.QJsonDocument.
fromBinaryData
(
data
[
,
validation=Validate
]
)
¶
data
–
QByteArray
validation
–
DataValidation
注意
此函数被弃用。
创建
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.
另请参阅
toBinaryData()
fromRawData()
isNull()
DataValidation
QCborValue
PySide2.QtCore.QJsonDocument.
fromJson
(
json
[
,
error=None
]
)
¶
json
–
QByteArray
error
–
QJsonParseError
剖析
json
作为 UTF-8 编码 JSON 文档,并创建
QJsonDocument
从它。
返回有效 (非 null)
QJsonDocument
若剖析成功。若它失败,返回文档将为 null,且可选
error
变量将包含有关错误的进一步细节。
PySide2.QtCore.QJsonDocument.
fromRawData
(
data
,
size
[
,
validation=Validate
]
)
¶
data – str
size
–
int
validation
–
DataValidation
注意
此函数被弃用。
创建
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.
另请参阅
rawData()
fromBinaryData()
isNull()
DataValidation
QCborValue
PySide2.QtCore.QJsonDocument.
fromVariant
(
variant
)
¶
variant – object
创建
QJsonDocument
从
QVariant
variant
.
若
variant
包含任何其它类型除了
QVariantMap
,
QVariantHash
,
QVariantList
or
QStringList
,返回的文档无效。
另请参阅
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
PySide2.QtCore.QJsonDocument.operator[](i)
i
–
int
返回
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.
另请参阅
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
(
)
¶
注意
此函数被弃用。
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
(
)
¶
转换
QJsonDocument
成缩进的 UTF-8 编码 JSON 文档。
另请参阅
PySide2.QtCore.QJsonDocument.
toJson
(
format
)
¶
format
–
JsonFormat
转换
QJsonDocument
成 UTF-8 编码 JSON 文档在提供
format
.
另请参阅
fromJson()
JsonFormat
PySide2.QtCore.QJsonDocument.
toVariant
(
)
¶
object
返回
QVariant
表示 Json 文档。
返回的变体将是
QVariantList
若文档为
QJsonArray
和
QVariantMap
若文档为
QJsonObject
.
另请参阅