PySide6.QtCore.QJsonDocument¶
- class QJsonDocument¶
- The - QJsonDocumentclass 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¶- QJsonDocumentis 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 - QJsonDocumentusing- 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.- 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 - QJsonDocumentusing- toJson().- 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 - QJsonDocumentfrom- array.- __init__(other)
- Parameters:
- other – - QJsonDocument
 
 - Creates a copy of the - otherdocument.- __init__(object)
- Parameters:
- object – - QJsonObject
 
 - Creates a - QJsonDocumentfrom- object.- array()¶
- Return type:
 
 - Returns the - QJsonArraycontained 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 - jsonas a UTF-8 encoded JSON document, and creates a- QJsonDocumentfrom it.- Returns a valid (non-null) - QJsonDocumentif the parsing succeeds. If it fails, the returned document will be null, and the optional- errorvariable will contain further details about the error.- See also - static fromVariant(variant)¶
- Parameters:
- variant – object 
- Return type:
 
 - Creates a - QJsonDocumentfrom the- QVariant- variant.- If the - variantcontains any other type than a QVariantMap , QVariantHash , QVariantList or- QStringList, the returned document is invalid.- See also - isArray()¶
- Return type:
- bool 
 
 - Returns - trueif the document contains an array.- See also - isEmpty()¶
- Return type:
- bool 
 
 - Returns - trueif the document doesn’t contain any data.- isNull()¶
- Return type:
- bool 
 
 - returns - trueif 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 - trueif the document contains an object.- object()¶
- Return type:
- QJsonObject
 
 - Returns the - QJsonObjectcontained in the document.- Returns an empty object if the document contains an array. - See also - __ne__(rhs)¶
- Parameters:
- rhs – - QJsonDocument
- Return type:
- bool 
 
 - Returns - trueif the- lhsdocument is not equal to- rhsdocument,- falseotherwise.- __eq__(rhs)¶
- Parameters:
- rhs – - QJsonDocument
- Return type:
- bool 
 
 - Returns - trueif the- lhsdocument is equal to- rhsdocument,- falseotherwise.- 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 - QJsonValuerepresenting the value for the key- key.- Equivalent to calling - object().value(key).- The returned - QJsonValueis- Undefinedif the key does not exist, or if- isObject()is false.- See also - QJsonValue- isUndefined()- QJsonObject- operator(i)
- Parameters:
- i – int 
- Return type:
 
 - Returns a - QJsonValuerepresenting the value for index- i.- Equivalent to calling - array().at(i).- The returned - QJsonValueis- Undefined, if- iis out of bounds, or if- isArray()is false.- See also - setArray(array)¶
- Parameters:
- array – - QJsonArray
 
 - Sets - arrayas the main object of this document.- See also - setObject(object)¶
- Parameters:
- object – - QJsonObject
 
 - Sets - objectas the main object of this document.- See also - swap(other)¶
- Parameters:
- other – - QJsonDocument
 
 - Swaps this document with - other. This operation is very fast and never fails.- toJson([format=QJsonDocument.JsonFormat.Indented])¶
- Parameters:
- format – - JsonFormat
- Return type:
 
 - Converts the - QJsonDocumentto a UTF-8 encoded JSON document in the provided- format.- See also - toVariant()¶
- Return type:
- object 
 
 - Returns a - QVariantrepresenting the Json document.- The returned variant will be a QVariantList if the document is a - QJsonArrayand a QVariantMap if the document is a- QJsonObject.- See also