The QJsonDocument class provides a way to read and write JSON documents. More...
Header: | #include <QJsonDocument> |
qmake: | QT += core |
Since: | Qt 5.0 |
This class was introduced in Qt 5.0.
Note: All functions in this class are reentrant.
enum | DataValidation { Validate, BypassValidation } |
enum | JsonFormat { Indented, Compact } |
QJsonDocument(QJsonDocument &&other) | |
QJsonDocument(const QJsonDocument &other) | |
QJsonDocument(const QJsonArray &array) | |
QJsonDocument(const QJsonObject &object) | |
QJsonDocument() | |
QJsonDocument & | operator=(QJsonDocument &&other) |
QJsonDocument & | operator=(const QJsonDocument &other) |
~QJsonDocument() | |
QJsonArray | array() const |
bool | isArray() const |
bool | isEmpty() const |
bool | isNull() const |
bool | isObject() const |
QJsonObject | object() const |
void | setArray(const QJsonArray &array) |
void | setObject(const QJsonObject &object) |
void | swap(QJsonDocument &other) |
QByteArray | toJson() const |
QByteArray | toJson(QJsonDocument::JsonFormat format) const |
QVariant | toVariant() const |
bool | operator!=(const QJsonDocument &other) const |
bool | operator==(const QJsonDocument &other) const |
const QJsonValue | operator[](const QString &key) const |
const QJsonValue | operator[](QStringView key) const |
const QJsonValue | operator[](QLatin1String key) const |
const QJsonValue | operator[](int i) const |
QJsonDocument | fromJson(const QByteArray &json, QJsonParseError *error = nullptr) |
QJsonDocument | fromVariant(const QVariant &variant) |
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 using 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().
See also JSON Support in Qt and JSON Save Game Example.
This value is used to tell QJsonDocument whether to validate the binary data when converting to a QJsonDocument using fromBinaryData() or fromRawData().
Constant | Value | Description |
---|---|---|
QJsonDocument::Validate |
0 |
Validate the data before using it. This is the default. |
QJsonDocument::BypassValidation |
1 |
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. |
This value defines the format of the JSON byte array produced when converting to a QJsonDocument using toJson().
Constant | Value | Description |
---|---|---|
QJsonDocument::Indented |
0 |
Defines human readable output as follows:{ "Array": [ true, 999, "string" ], "Key": "Value", "null": null } |
QJsonDocument::Compact |
1 |
Defines a compact output as follows:{"Array":[true,999,"string"],"Key":"Value","null":null} |
This enum was introduced or modified in Qt 5.1.
Move-constructs a QJsonDocument from other.
This function was introduced in Qt 5.10.
Creates a copy of the other document.
Constructs a QJsonDocument from array.
Creates a QJsonDocument from object.
Constructs an empty and invalid document.
Move-assigns other to this document.
This function was introduced in Qt 5.10.
Assigns the other document to this QJsonDocument. Returns a reference to this object.
Deletes the document.
Binary data set with fromRawData is not freed.
Returns the QJsonArray contained in the document.
Returns an empty array if the document contains an object.
See also isArray(), object(), and setArray().
[static]
QJsonDocument QJsonDocument::fromJson(const QByteArray &json, QJsonParseError *error = nullptr)
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 toJson(), QJsonParseError, and isNull().
[static]
QJsonDocument QJsonDocument::fromVariant(const QVariant &variant)
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 toVariant().
Returns true
if the document contains an array.
See also array() and isObject().
Returns true
if the document doesn't contain any data.
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.
Returns true
if the document contains an object.
See also object() and isArray().
Returns the QJsonObject contained in the document.
Returns an empty object if the document contains an array.
See also isObject(), array(), and setObject().
Sets array as the main object of this document.
See also setObject() and array().
Sets object as the main object of this document.
See also setArray() and object().
Swaps the document other with this. This operation is very fast and never fails.
This function was introduced in Qt 5.10.
Converts the QJsonDocument to an indented, UTF-8 encoded JSON document.
See also fromJson().
Converts the QJsonDocument to a UTF-8 encoded JSON document in the provided format.
This function was introduced in Qt 5.1.
See also fromJson() and JsonFormat.
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 fromVariant() and QJsonValue::toVariant().
returns true
if other is not equal to this document
Returns true
if the other document is equal to this document.
Returns a QJsonValue representing the value for the key key.
Equivalent to calling object().value(key).
The returned QJsonValue is QJsonValue::Undefined if the key does not exist, or if isObject() is false.
This function was introduced in Qt 5.10.
See also QJsonValue, QJsonValue::isUndefined(), and QJsonObject.
This is an overloaded function.
This function was introduced in Qt 5.14.
This is an overloaded function.
This function was introduced in Qt 5.10.
Returns a QJsonValue representing the value for index i.
Equivalent to calling array().at(i).
The returned QJsonValue is QJsonValue::Undefined, if i is out of bounds, or if isArray() is false.
This function was introduced in Qt 5.10.
See also QJsonValue, QJsonValue::isUndefined(), and QJsonArray.
© The Qt Company Ltd
Licensed under the GNU Free Documentation License, Version 1.3.
https://doc.qt.io/qt-5.15/qjsondocument.html