QJsonDocument Class

Die Klasse QJsonDocument bietet eine Möglichkeit, JSON-Dokumente zu lesen und zu schreiben. Mehr...

Kopfzeile: #include <QJsonDocument>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

Diese Klasse ist gleichwertig-vergleichbar.

Hinweis: Alle Funktionen in dieser Klasse sind reentrant.

Öffentliche Typen

enum JsonFormat { Indented, Compact }

Öffentliche Funktionen

QJsonDocument()
QJsonDocument(const QJsonArray &array)
QJsonDocument(const QJsonObject &object)
QJsonDocument(const QJsonDocument &other)
QJsonDocument(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(QJsonDocument::JsonFormat format = Indented) const
QVariant toVariant() const
QJsonDocument &operator=(QJsonDocument &&other)
QJsonDocument &operator=(const QJsonDocument &other)
const QJsonValue operator[](const QString &key) const
const QJsonValue operator[](qsizetype i) const
const QJsonValue operator[](QLatin1StringView key) const
const QJsonValue operator[](QStringView key) const

Statische öffentliche Mitglieder

QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error = nullptr)
QJsonDocument fromVariant(const QVariant &variant)
bool operator!=(const QJsonDocument &lhs, const QJsonDocument &rhs)
bool operator==(const QJsonDocument &lhs, const QJsonDocument &rhs)

Detaillierte Beschreibung

QJsonDocument ist eine Klasse, die ein vollständiges JSON-Dokument umhüllt und dieses Dokument aus einer UTF-8-kodierten textbasierten Darstellung lesen und in diese schreiben kann.

Ein JSON-Dokument kann mit QJsonDocument::fromJson() von seiner textbasierten Darstellung in ein QJsonDocument konvertiert werden. toJson() konvertiert es zurück in Text. Der Parser ist sehr schnell und effizient und konvertiert das JSON in die von Qt verwendete binäre Darstellung.

Die Gültigkeit des geparsten Dokuments kann mit !isNull() abgefragt werden.

Ein Dokument kann mit isArray() und isObject() daraufhin abgefragt werden, ob es ein Array oder ein Objekt enthält. Das im Dokument enthaltene Array oder Objekt kann mit array() oder object() abgefragt und dann gelesen oder manipuliert werden.

Siehe auch JSON-Unterstützung in Qt und Speichern und Laden eines Spiels.

Dokumentation der Mitgliedstypen

enum QJsonDocument::JsonFormat

Dieser Wert definiert das Format des JSON-Byte-Arrays, das bei der Konvertierung in ein QJsonDocument mit toJson() erzeugt wird.

KonstanteWertBeschreibung
QJsonDocument::Indented0Definiert die menschenlesbare Ausgabe wie folgt:
    {
        "Array": [
            true,
            999,
            "string"
        ],
        "Key": "Value",
        "null": null
    }
QJsonDocument::Compact1Legt eine kompakte Ausgabe wie folgt fest:
    {"Array":[true,999,"string"],"Key":"Value","null":null}

Mitgliederfunktion Dokumentation

QJsonDocument::QJsonDocument()

Konstruiert ein leeres und ungültiges Dokument.

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

Konstruiert ein QJsonDocument aus array.

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

Erzeugt ein QJsonDocument aus object.

QJsonDocument::QJsonDocument(const QJsonDocument &other)

Erzeugt eine Kopie des Dokuments other.

[noexcept] QJsonDocument::QJsonDocument(QJsonDocument &&other)

Move-konstruiert ein QJsonDocument aus other.

[noexcept] QJsonDocument::~QJsonDocument()

Löscht das Dokument.

Binärer Datensatz mit fromRawData wird nicht freigegeben.

QJsonArray QJsonDocument::array() const

Gibt die im Dokument enthaltenen QJsonArray zurück.

Gibt ein leeres Array zurück, wenn das Dokument ein Objekt enthält.

Siehe auch isArray(), object(), und setArray().

[static] QJsonDocument QJsonDocument::fromJson(const QByteArray &json, QJsonParseError *error = nullptr)

Analysiert json als UTF-8-kodiertes JSON-Dokument und erstellt daraus ein QJsonDocument.

Gibt ein gültiges (nicht-null) QJsonDocument zurück, wenn das Parsen erfolgreich war. Schlägt das Parsen fehl, ist das zurückgegebene Dokument null, und die optionale Variable error enthält weitere Details über den Fehler.

Siehe auch toJson(), QJsonParseError, und isNull().

[static] QJsonDocument QJsonDocument::fromVariant(const QVariant &variant)

Erzeugt ein QJsonDocument aus dem QVariant variant .

Wenn variant einen anderen Typ als QVariantMap, QVariantHash, QVariantList oder QStringList enthält, ist das zurückgegebene Dokument ungültig.

Siehe auch toVariant().

bool QJsonDocument::isArray() const

Gibt true zurück, wenn das Dokument ein Array enthält.

Siehe auch array() und isObject().

bool QJsonDocument::isEmpty() const

Gibt true zurück, wenn das Dokument keine Daten enthält.

bool QJsonDocument::isNull() const

gibt true zurück, wenn dieses Dokument null ist.

Null-Dokumente sind Dokumente, die mit dem Standardkonstruktor erstellt wurden.

Dokumente, die aus UTF-8-kodiertem Text oder dem Binärformat erstellt werden, werden während des Parsings validiert. Wenn die Validierung fehlschlägt, ist das zurückgegebene Dokument ebenfalls null.

bool QJsonDocument::isObject() const

Gibt true zurück, wenn das Dokument ein Objekt enthält.

Siehe auch object() und isArray().

QJsonObject QJsonDocument::object() const

Gibt die im Dokument enthaltene QJsonObject zurück.

Gibt ein leeres Objekt zurück, wenn das Dokument ein Array enthält.

Siehe auch isObject(), array(), und setObject().

void QJsonDocument::setArray(const QJsonArray &array)

Legt array als das Hauptobjekt dieses Dokuments fest.

Siehe auch setObject() und array().

void QJsonDocument::setObject(const QJsonObject &object)

Legt object als das Hauptobjekt dieses Dokuments fest.

Siehe auch setArray() und object().

[noexcept] void QJsonDocument::swap(QJsonDocument &other)

Tauscht dieses Dokument mit other aus. Dieser Vorgang ist sehr schnell und schlägt nie fehl.

QByteArray QJsonDocument::toJson(QJsonDocument::JsonFormat format = Indented) const

Konvertiert das QJsonDocument in ein UTF-8 kodiertes JSON-Dokument in der angegebenen format.

Siehe auch fromJson() und JsonFormat.

QVariant QJsonDocument::toVariant() const

Gibt ein QVariant zurück, das das Json-Dokument darstellt.

Die zurückgegebene Variante ist QVariantList, wenn das Dokument ein QJsonArray ist, und QVariantMap, wenn das Dokument ein QJsonObject ist.

Siehe auch fromVariant() und QJsonValue::toVariant().

[noexcept] QJsonDocument &QJsonDocument::operator=(QJsonDocument &&other)

Verschieben - weist diesem Dokument die Adresse other zu.

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

Weist das Dokument other diesem QJsonDocument zu. Gibt einen Verweis auf dieses Objekt zurück.

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

Gibt ein QJsonValue zurück, das den Wert für den Schlüssel key darstellt.

Entspricht dem Aufruf von object().value(key).

Der zurückgegebene QJsonValue ist QJsonValue::Undefined, wenn der Schlüssel nicht existiert oder wenn isObject() falsch ist.

Siehe auch QJsonValue, QJsonValue::isUndefined(), und QJsonObject.

const QJsonValue QJsonDocument::operator[](qsizetype i) const

Gibt ein QJsonValue zurück, das den Wert für den Index i darstellt.

Äquivalent zum Aufruf von array().at(i).

Der zurückgegebene QJsonValue ist QJsonValue::Undefined, wenn i außerhalb der Grenzen liegt oder wenn isArray() falsch ist.

Siehe auch QJsonValue, QJsonValue::isUndefined(), und QJsonArray.

const QJsonValue QJsonDocument::operator[](QLatin1StringView key) const

Dies ist eine überladene Funktion.

const QJsonValue QJsonDocument::operator[](QStringView key) const

Dies ist eine überladene Funktion.

Verwandte Nicht-Mitglieder

[noexcept] bool operator!=(const QJsonDocument &lhs, const QJsonDocument &rhs)

Gibt true zurück, wenn das Dokument lhs nicht mit dem Dokument rhs übereinstimmt, andernfalls false.

[noexcept] bool operator==(const QJsonDocument &lhs, const QJsonDocument &rhs)

Gibt true zurück, wenn das Dokument lhs gleich dem Dokument rhs ist, andernfalls false.

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.