- class QJsonDocument¶
The
QJsonDocument
class provides a way to read and write JSON documents. More…Synopsis¶
Methods¶
def
__init__()
def
array()
def
isArray()
def
isEmpty()
def
isNull()
def
isObject()
def
object()
def
__ne__()
def
__eq__()
def
operator[]()
def
setArray()
def
setObject()
def
swap()
def
toJson()
def
toVariant()
Static functions¶
def
fromJson()
def
fromVariant()
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
usingfromJson()
.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.See also
JSON Support in Qt Saving and Loading a Game
- class 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
usingtoJson()
.Constant
Description
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}
- __init__()¶
Constructs an empty and invalid document.
- __init__(array)
- Parameters:
array –
QJsonArray
Constructs a
QJsonDocument
fromarray
.- __init__(other)
- Parameters:
other –
QJsonDocument
Creates a copy of the
other
document.- __init__(object)
- Parameters:
object –
QJsonObject
Creates a
QJsonDocument
fromobject
.- array()¶
- Return type:
Returns the
QJsonArray
contained in the document.Returns an empty array if the document contains an object.
See also
- static fromJson(json[, error=None])¶
- Parameters:
json –
QByteArray
error –
QJsonParseError
- Return type:
Parses
json
as a UTF-8 encoded JSON document, and creates aQJsonDocument
from it.Returns a valid (non-null)
QJsonDocument
if the parsing succeeds. If it fails, the returned document will be null, and the optionalerror
variable will contain further details about the error.See also
- static fromVariant(variant)¶
- Parameters:
variant – object
- Return type:
Creates a
QJsonDocument
from theQVariant
variant
.If the
variant
contains any other type than a QVariantMap , QVariantHash , QVariantList orQStringList
, the returned document is invalid.See also
- isArray()¶
- Return type:
bool
Returns
true
if the document contains an array.See also
- isEmpty()¶
- Return type:
bool
Returns
true
if the document doesn’t contain any data.- 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.
- isObject()¶
- Return type:
bool
Returns
true
if the document contains an object.- object()¶
- Return type:
QJsonObject
Returns the
QJsonObject
contained in the document.Returns an empty object if the document contains an array.
See also
- __ne__(rhs)¶
- Parameters:
rhs –
QJsonDocument
- Return type:
bool
Returns
true
if thelhs
document is not equal torhs
document,false
otherwise.- __eq__(rhs)¶
- Parameters:
rhs –
QJsonDocument
- Return type:
bool
Returns
true
if thelhs
document is equal torhs
document,false
otherwise.- operator(key)¶
- Parameters:
key –
QLatin1String
- Return type:
- operator(key)
- Parameters:
key – str
- Return type:
This is an overloaded function.
- operator(key)
- Parameters:
key – str
- Return type:
Returns a
QJsonValue
representing the value for the keykey
.Equivalent to calling
object()
.value(key).The returned
QJsonValue
isUndefined
if the key does not exist, or ifisObject()
is false.See also
QJsonValue
isUndefined()
QJsonObject
- operator(i)
- Parameters:
i – int
- Return type:
Returns a
QJsonValue
representing the value for indexi
.Equivalent to calling
array()
.at(i).The returned
QJsonValue
isUndefined
, ifi
is out of bounds, or ifisArray()
is false.See also
- setArray(array)¶
- Parameters:
array –
QJsonArray
Sets
array
as the main object of this document.See also
- setObject(object)¶
- Parameters:
object –
QJsonObject
Sets
object
as the main object of this document.See also
- swap(other)¶
- Parameters:
other –
QJsonDocument
Swaps the document
other
with this. This operation is very fast and never fails.- toJson([format=QJsonDocument.JsonFormat.Indented])¶
- Parameters:
format –
JsonFormat
- Return type:
Converts the
QJsonDocument
to a UTF-8 encoded JSON document in the providedformat
.See also
- 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 aQJsonObject
.See also