QJsonDocument#
The QJsonDocument
class provides a way to read and write JSON documents. More…
Synopsis#
Functions#
def
array
()def
isArray
()def
isEmpty
()def
isNull
()def
isObject
()def
object
()def
__ne__
(other)def
__eq__
(other)def
operator[]
(key)def
operator[]
(key)def
operator[]
(i)def
setArray
(array)def
setObject
(object)def
swap
(other)def
toJson
([format=QJsonDocument.JsonFormat.Indented])def
toVariant
()
Static functions#
def
fromJson
(json[, error=None])def
fromVariant
(variant)
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Detailed Description#
QJsonDocument
is a class that wraps a complete JSON document and can read this document from, and write it to, a UTF-8 encoded text-based representation.
A JSON document can be converted from its text-based representation to a QJsonDocument
using 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.
- class PySide6.QtCore.QJsonDocument#
PySide6.QtCore.QJsonDocument(array)
PySide6.QtCore.QJsonDocument(other)
PySide6.QtCore.QJsonDocument(object)
- Parameters:
other –
PySide6.QtCore.QJsonDocument
object –
QJsonObject
array –
PySide6.QtCore.QJsonArray
Constructs an empty and invalid document.
Constructs a QJsonDocument
from array
.
Creates a copy of the other
document.
Creates a QJsonDocument
from object
.
- PySide6.QtCore.QJsonDocument.JsonFormat#
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
This value defines the format of the JSON byte array produced when converting to a QJsonDocument
using toJson()
.
Constant
Description
QJsonDocument.Indented
Defines human readable output as follows:
"Array": [ True, 999, "string" ], "Key": "Value", "null": nullQJsonDocument.Compact
Defines a compact output as follows:
{"Array":[True,999,"string"],"Key":"Value","null":null}
- PySide6.QtCore.QJsonDocument.array()#
- Return type:
Returns the QJsonArray
contained in the document.
Returns an empty array if the document contains an object.
See also
- static PySide6.QtCore.QJsonDocument.fromJson(json[, error=None])#
- Parameters:
json –
PySide6.QtCore.QByteArray
error –
PySide6.QtCore.QJsonParseError
- Return type:
Parses json
as a UTF-8 encoded JSON document, and creates a QJsonDocument
from it.
Returns a valid (non-null) QJsonDocument
if the parsing succeeds. If it fails, the returned document will be null, and the optional error
variable will contain further details about the error.
See also
- static PySide6.QtCore.QJsonDocument.fromVariant(variant)#
- Parameters:
variant – object
- Return type:
Creates a QJsonDocument
from the QVariant
variant
.
If the variant
contains any other type than a QVariantMap , QVariantHash , QVariantList or QStringList
, the returned document is invalid.
See also
- PySide6.QtCore.QJsonDocument.isArray()#
- Return type:
bool
Returns true
if the document contains an array.
See also
- PySide6.QtCore.QJsonDocument.isEmpty()#
- Return type:
bool
Returns true
if the document doesn’t contain any data.
- PySide6.QtCore.QJsonDocument.isNull()#
- Return type:
bool
returns true
if this document is null.
Null documents are documents created through the default constructor.
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.
- PySide6.QtCore.QJsonDocument.isObject()#
- Return type:
bool
Returns true
if the document contains an object.
- PySide6.QtCore.QJsonDocument.object()#
- Return type:
QJsonObject
Returns the QJsonObject
contained in the document.
Returns an empty object if the document contains an array.
See also
- PySide6.QtCore.QJsonDocument.__ne__(other)#
- Parameters:
other –
PySide6.QtCore.QJsonDocument
- Return type:
bool
returns true
if other
is not equal to this document
- PySide6.QtCore.QJsonDocument.__eq__(other)#
- Parameters:
other –
PySide6.QtCore.QJsonDocument
- Return type:
bool
Returns true
if the other
document is equal to this document.
- PySide6.QtCore.QJsonDocument.operator(key)#
- Parameters:
key – str
- Return type:
This is an overloaded function.
- PySide6.QtCore.QJsonDocument.operator(key)
- Parameters:
key – str
- Return type:
Returns a QJsonValue
representing the value for the key key
.
Equivalent to calling object()
.value(key).
The returned QJsonValue
is Undefined
if the key does not exist, or if isObject()
is false.
See also
QJsonValue
isUndefined()
QJsonObject
- PySide6.QtCore.QJsonDocument.operator(i)
- Parameters:
i – int
- Return type:
Returns a QJsonValue
representing the value for index i
.
Equivalent to calling array()
.at(i).
The returned QJsonValue
is Undefined
, if i
is out of bounds, or if isArray()
is false.
See also
- PySide6.QtCore.QJsonDocument.setArray(array)#
- Parameters:
array –
PySide6.QtCore.QJsonArray
Sets array
as the main object of this document.
See also
- PySide6.QtCore.QJsonDocument.setObject(object)#
- Parameters:
object –
QJsonObject
Sets object
as the main object of this document.
See also
- PySide6.QtCore.QJsonDocument.swap(other)#
- Parameters:
other –
PySide6.QtCore.QJsonDocument
Swaps the document other
with this. This operation is very fast and never fails.
- PySide6.QtCore.QJsonDocument.toJson([format=QJsonDocument.JsonFormat.Indented])#
- Parameters:
format –
JsonFormat
- Return type:
Converts the QJsonDocument
to a UTF-8 encoded JSON document in the provided format
.
See also
fromJson()
JsonFormat
- PySide6.QtCore.QJsonDocument.toVariant()#
- Return type:
object
Returns a QVariant
representing the Json document.
The returned variant will be a QVariantList if the document is a QJsonArray
and a QVariantMap if the document is a QJsonObject
.
See also