IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

QJsonDocument Class

The QJsonDocument class provides a way to read and write JSON documents.

This class was introduced in Qt 5.0.

All functions in this class are reentrant.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

QJsonDocument Class▲

  • Header: QJsonDocument

  • Since: Qt 5.0

  • CMake:

    find_package(Qt6 REQUIRED COMPONENTS Core)

    target_link_libraries(mytarget PRIVATE Qt6::Core)

  • qmake: QT += core

  • Group: QJsonDocument is part of JSON Support in Qt, Implicitly Shared Classes, qtserialization

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 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.

See Also▲

Member Type Documentation▲

 

[since 5.1] enum QJsonDocument::JsonFormat▲

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:

 
Sélectionnez
    {
        "Array": [
            true,
            999,
            "string"
        ],
        "Key": "Value",
        "null": null
    }

QJsonDocument::Compact

1

Defines a compact output as follows:

 
Sélectionnez
    {"Array":[true,999,"string"],"Key":"Value","null":null}

This enum was introduced or modified in Qt 5.1.

Member Function Documentation▲

 

QJsonDocument::QJsonDocument()▲

Constructs an empty and invalid document.

[explicit] QJsonDocument::QJsonDocument(const QJsonObject &object)▲

Creates a QJsonDocument from object.

[explicit] QJsonDocument::QJsonDocument(const QJsonArray &array)▲

Constructs a QJsonDocument from array.

QJsonDocument::QJsonDocument(const QJsonDocument &other)▲

Creates a copy of the other document.

[since 5.10] QJsonDocument::QJsonDocument(QJsonDocument &&other)▲

Move-constructs a QJsonDocument from other.

This function was introduced in Qt 5.10.

QJsonDocument::~QJsonDocument()▲

Deletes the document.

Binary data set with fromRawData is not freed.

QJsonArray QJsonDocument::array() const▲

Returns the QJsonArray contained in the document.

Returns an empty array if the document contains an object.

See Also▲

See also isArray(), object(), 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▲

See also toJson(), QJsonParseError, 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▲

See also toVariant()

bool QJsonDocument::isArray() const▲

Returns true if the document contains an array.

See Also▲

See also array(), isObject()

bool QJsonDocument::isEmpty() const▲

Returns true if the document doesn't contain any data.

bool QJsonDocument::isNull() const▲

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.

bool QJsonDocument::isObject() const▲

Returns true if the document contains an object.

See Also▲

See also object(), isArray()

QJsonObject QJsonDocument::object() const▲

Returns the QJsonObject contained in the document.

Returns an empty object if the document contains an array.

See Also▲

See also isObject(), array(), setObject()

void QJsonDocument::setArray(const QJsonArray &array)▲

Sets array as the main object of this document.

See Also▲

See also setObject(), array()

void QJsonDocument::setObject(const QJsonObject &object)▲

Sets object as the main object of this document.

See Also▲

See also setArray(), object()

[since 5.10] void QJsonDocument::swap(QJsonDocument &other)▲

Swaps the document other with this. This operation is very fast and never fails.

This function was introduced in Qt 5.10.

[since 5.1] QByteArray QJsonDocument::toJson(QJsonDocument::JsonFormat format = Indented) const▲

Converts the QJsonDocument to a UTF-8 encoded JSON document in the provided format.

This function was introduced in Qt 5.1.

See Also▲

See also fromJson(), JsonFormat

QVariant QJsonDocument::toVariant() const▲

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▲

bool QJsonDocument::operator!=(const QJsonDocument &other) const▲

returns true if other is not equal to this document

QJsonDocument &QJsonDocument::operator=(const QJsonDocument &other)▲

Assigns the other document to this QJsonDocument. Returns a reference to this object.

[since 5.10] QJsonDocument &QJsonDocument::operator=(QJsonDocument &&other)▲

Move-assigns other to this document.

This function was introduced in Qt 5.10.

bool QJsonDocument::operator==(const QJsonDocument &other) const▲

Returns true if the other document is equal to this document.

[since 5.10] const QJsonValue QJsonDocument::operator[](const QString &key) const▲

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▲

[since 5.14] const QJsonValue QJsonDocument::operator[](QStringView key) const▲

This is an overloaded function.

This function was introduced in Qt 5.14.

[since 5.10] const QJsonValue QJsonDocument::operator[](QLatin1StringView key) const▲

This is an overloaded function.

This function was introduced in Qt 5.10.

[since 5.10] const QJsonValue QJsonDocument::operator[](qsizetype i) const▲

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▲

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+