In questa pagina

QJsonDocument Class

La classe QJsonDocument consente di leggere e scrivere documenti JSON. Per saperne di più...

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

Nota: tutte le funzioni di questa classe sono rientranti.

Confronto tra QJsonDocument

CategoriaTipi comparabili
uguaglianzaQJsonDocument

Tipi pubblici

enum JsonFormat { Indented, Compact }

Funzioni pubbliche

QJsonDocument()
(since 6.12) QJsonDocument(QJsonArray &&array)
(since 6.12) QJsonDocument(QJsonObject &&object)
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 = JsonFormat::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

Membri pubblici statici

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)

Descrizione dettagliata

QJsonDocument è una classe che incapsula un documento JSON completo ed è in grado di leggere e scrivere tale documento da e verso una rappresentazione testuale codificata in UTF-8.

Un documento JSON può essere convertito dalla sua rappresentazione testuale in un QJsonDocument utilizzando QJsonDocument::fromJson(). toJson() lo riconverte in testo. Il parser è molto veloce ed efficiente e converte il JSON nella rappresentazione binaria utilizzata da Qt.

È possibile verificare la validità del documento analizzato con !isNull()

È possibile verificare se un documento contiene un array o un oggetto utilizzando isArray() e isObject(). L’array o l’oggetto contenuto nel documento può essere recuperato utilizzando array() o object() e quindi letto o modificato.

Vedi anche Supporto JSON in Qt e Salvataggio e caricamento di una partita.

Documentazione sui tipi dei membri

enum QJsonDocument::JsonFormat

Questo valore definisce il formato dell’array di byte JSON generato durante la conversione in un oggetto ` QJsonDocument ` tramite la funzione ` toJson()`.

CostanteValoreDescrizione
QJsonDocument::Indented0Definisce un output leggibile dall'utente come segue:
    {
        "Array": [
            true,
            999,
            "string"
        ],
        "Key": "Value",
        "null": null
    }
QJsonDocument::Compact1Definisce un output compatto come segue:
    {"Array":[true,999,"string"],"Key":"Value","null":null}

Documentazione delle funzioni membro

QJsonDocument::QJsonDocument()

Crea un documento vuoto e non valido.

[explicit, since 6.12] QJsonDocument::QJsonDocument(QJsonArray &&array)

Crea un oggetto QJsonDocument a partire da un array.

Questa funzione è stata introdotta in Qt 6.12.

[explicit, since 6.12] QJsonDocument::QJsonDocument(QJsonObject &&object)

Crea un oggetto `QJsonDocument` da un oggetto ` object`.

Questa funzione è stata introdotta in Qt 6.12.

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

Crea un oggetto QJsonDocument a partire da array.

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

Crea un oggetto QJsonDocument a partire da un object.

QJsonDocument::QJsonDocument(const QJsonDocument &other)

Crea una copia del documento " other ".

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

Crea un oggetto QJsonDocument a partire da other.

[noexcept] QJsonDocument::~QJsonDocument()

Elimina il documento.

Il set di dati binari creato con fromRawData non viene liberato.

QJsonArray QJsonDocument::array() const

Restituisce l'QJsonArray e contenuto nel documento.

Restituisce un array vuoto se il documento contiene un oggetto.

Vedi anche ` isArray()`, ` object()` e ` setArray()`.

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

Analizza json come documento JSON codificato in UTF-8 e crea un oggetto QJsonDocument a partire da esso.

Restituisce un oggetto ` QJsonDocument ` valido (non nullo) se l'analisi ha esito positivo. In caso di errore, il documento restituito sarà nullo e la variabile opzionale ` error ` conterrà ulteriori dettagli sull'errore.

Vedi anche toJson(), QJsonParseError e isNull().

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

Crea un file " QJsonDocument " a partire dal file " QVariant " variant.

Se l'variant contiene un tipo diverso da QVariantMap, QVariantHash, QVariantList o QStringList, il documento restituito non è valido.

Vedi anche toVariant().

bool QJsonDocument::isArray() const

Restituisce true se il documento contiene un array.

Vedi anche array() e isObject().

bool QJsonDocument::isEmpty() const

Restituisce " true " se il documento non contiene dati.

bool QJsonDocument::isNull() const

restituisce ` true ` se questo documento è nullo.

I documenti null sono documenti creati tramite il costruttore predefinito.

I documenti creati a partire da testo codificato in UTF-8 o dal formato binario vengono convalidati durante l'analisi. Se la convalida fallisce, anche il documento restituito sarà nullo.

bool QJsonDocument::isObject() const

Restituisce true se il documento contiene un oggetto.

Vedi anche object() e isArray().

QJsonObject QJsonDocument::object() const

Restituisce l'QJsonObject e contenuto nel documento.

Restituisce un oggetto vuoto se il documento contiene un array.

Vedi anche isObject(), array() e setObject().

void QJsonDocument::setArray(const QJsonArray &array)

Imposta array come oggetto principale di questo documento.

Vedi anche setObject() e array().

void QJsonDocument::setObject(const QJsonObject &object)

Imposta object come oggetto principale di questo documento.

Vedi anche setArray() e object().

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

Sostituisce questo documento con other. Questa operazione è molto veloce e non fallisce mai.

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

Converte l'QJsonDocument in un documento JSON codificato in UTF-8 nel file format specificato.

Vedi anche fromJson() e JsonFormat.

QVariant QJsonDocument::toVariant() const

Restituisce un oggetto ` QVariant ` che rappresenta il documento JSON.

La variante restituita sarà un ` QVariantList ` se il documento è un ` QJsonArray ` e un ` QVariantMap ` se il documento è un ` QJsonObject`.

Vedi anche fromVariant() e QJsonValue::toVariant().

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

Move assegna a questo documento il numero di riferimento " other ".

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

Assegna il documento other a questo oggetto QJsonDocument. Restituisce un riferimento a questo oggetto.

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

Restituisce un oggetto ` QJsonValue ` che rappresenta il valore associato alla chiave ` key`.

Equivalente alla chiamata a ` object()`.value(key).

L'QJsonValue restituito è QJsonValue::Undefined se la chiave non esiste o se isObject() restituisce false.

Vedi anche QJsonValue, QJsonValue::isUndefined() e QJsonObject.

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

Restituisce un oggetto ` QJsonValue ` che rappresenta il valore corrispondente all'indice ` i`.

Equivalente alla chiamata a ` array().at(i)`.

L'QJsonValue restituito è QJsonValue::Undefined, se i è fuori intervallo o se isArray() restituisce false.

Vedi anche QJsonValue, QJsonValue::isUndefined() e QJsonArray.

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

Questa è una funzione sovraccaricata.

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

Questa è una funzione sovraccaricata.

Elementi correlati non membri

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

Restituisce ` true ` se il documento ` lhs ` non è uguale al documento ` rhs `, altrimenti restituisce ` false `.

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

Restituisce ` true ` se il documento ` lhs ` è uguale al documento ` rhs `, altrimenti restituisce ` false `.

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