In questa pagina

QJsonObject Class

La classe QJsonObject incapsula un oggetto JSON. Per saperne di più...

Intestazione: #include <QJsonObject>
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 QJsonObject

CategoriaTipi comparabili
uguaglianzaQJsonObject
uguaglianzaQJsonValue e QJsonValueConstRef.

Tipi pubblici

Funzioni pubbliche

QJsonObject()
QJsonObject(std::initializer_list<std::pair<QString, QJsonValue>> args)
QJsonObject(const QJsonObject &other)
QJsonObject(QJsonObject &&other)
~QJsonObject()
(since 6.10) auto asKeyValueRange() &&
(since 6.10) auto asKeyValueRange() &
(since 6.10) auto asKeyValueRange() const &&
(since 6.10) auto asKeyValueRange() const &
QJsonObject::iterator begin()
QJsonObject::const_iterator begin() const
(since 6.12) QJsonObject::const_iterator cbegin() const
(since 6.12) QJsonObject::const_iterator cend() const
QJsonObject::const_iterator constBegin() const
QJsonObject::const_iterator constEnd() const
QJsonObject::const_iterator constFind(const QString &key) const
QJsonObject::const_iterator constFind(QLatin1StringView key) const
QJsonObject::const_iterator constFind(QStringView key) const
(since 6.10) QJsonObject::const_key_value_iterator constKeyValueBegin() const
(since 6.10) QJsonObject::const_key_value_iterator constKeyValueEnd() const
bool contains(const QString &key) const
bool contains(QLatin1StringView key) const
bool contains(QStringView key) const
qsizetype count() const
bool empty() const
QJsonObject::iterator end()
QJsonObject::const_iterator end() const
QJsonObject::iterator erase(QJsonObject::iterator it)
QJsonObject::iterator find(const QString &key)
QJsonObject::iterator find(QLatin1StringView key)
QJsonObject::iterator find(QStringView key)
QJsonObject::const_iterator find(QLatin1StringView key) const
QJsonObject::const_iterator find(QStringView key) const
QJsonObject::const_iterator find(const QString &key) const
QJsonObject::iterator insert(const QString &key, const QJsonValue &value)
QJsonObject::iterator insert(QLatin1StringView key, const QJsonValue &value)
QJsonObject::iterator insert(QStringView key, const QJsonValue &value)
bool isEmpty() const
(since 6.10) QJsonObject::key_value_iterator keyValueBegin()
(since 6.10) QJsonObject::const_key_value_iterator keyValueBegin() const
(since 6.10) QJsonObject::key_value_iterator keyValueEnd()
(since 6.10) QJsonObject::const_key_value_iterator keyValueEnd() const
QStringList keys() const
qsizetype length() const
void remove(const QString &key)
void remove(QLatin1StringView key)
void remove(QStringView key)
qsizetype size() const
void swap(QJsonObject &other)
QJsonValue take(const QString &key)
QJsonValue take(QLatin1StringView key)
QJsonValue take(QStringView key)
QVariantHash toVariantHash() const
QVariantMap toVariantMap() const
QJsonValue value(const QString &key) const
QJsonValue value(QLatin1StringView key) const
QJsonValue value(QStringView key) const
QJsonObject &operator=(QJsonObject &&other)
QJsonObject &operator=(const QJsonObject &other)
QJsonValueRef operator[](const QString &key)
QJsonValue operator[](const QString &key) const
QJsonValueRef operator[](QLatin1StringView key)
QJsonValueRef operator[](QStringView key)
QJsonValue operator[](QLatin1StringView key) const
QJsonValue operator[](QStringView key) const

Membri pubblici statici

QJsonObject fromVariantHash(const QVariantHash &hash)
QJsonObject fromVariantMap(const QVariantMap &map)
bool operator!=(const QJsonObject &lhs, const QJsonObject &rhs)
bool operator==(const QJsonObject &lhs, const QJsonObject &rhs)

Descrizione dettagliata

Un oggetto JSON è un elenco di coppie chiave-valore, in cui le chiavi sono stringhe univoche e i valori sono rappresentati da un oggetto ` QJsonValue`.

Un QJsonObject può essere convertito da e verso un QVariantMap. È possibile interrogare il numero di coppie (chiave, valore) con le voce size(), insert() e remove() e iterare sul suo contenuto utilizzando il modello iteratore standard di C++.

QJsonObject è una classe condivisa implicitamente e condivide i dati con il documento da cui è stata creata fintanto che non viene modificata.

È possibile convertire l’oggetto da e verso JSON testuale tramite QJsonDocument.

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

Documentazione sui tipi dei membri

QJsonObject::ConstIterator

Sinonimo in stile Qt per " QJsonObject::const_iterator".

QJsonObject::Iterator

Sinonimo in stile Qt per " QJsonObject::iterator".

[since 6.10] QJsonObject::const_key_value_iterator

Il typedef QJsonObject::const_key_value_iterator fornisce un iteratore in stile STL per ` QJsonObject`.

QJsonObject::const_key_value_iterator è essenzialmente identico a QJsonObject::const_iterator, con la differenza che l'operatore *() restituisce una coppia chiave/valore anziché un singolo valore.

Questo typedef è stato introdotto in Qt 6.10.

Vedi anche ` QKeyValueIterator`.

QJsonObject::key_type

Definizione di tipo per ` QString`. Fornita per garantire la compatibilità con la STL.

[since 6.10] QJsonObject::key_value_iterator

Il typedef QJsonObject::key_value_iterator fornisce un iteratore in stile STL per ` QJsonObject`.

QJsonObject::key_value_iterator è sostanzialmente identico a ` QJsonObject::iterator `, con la differenza che l'operatore `*()` restituisce una coppia chiave/valore anziché un singolo valore.

Questo typedef è stato introdotto in Qt 6.10.

Vedi anche ` QKeyValueIterator`.

QJsonObject::mapped_type

Definizione di tipo per ` QJsonValue`. Fornita per garantire la compatibilità con la STL.

QJsonObject::size_type

Definizione di tipo per qsizetype. Fornita per garantire la compatibilità con la STL.

Documentazione sulle funzioni membro

QJsonObject::QJsonObject()

Crea un oggetto JSON vuoto.

Vedi anche ` isEmpty()`.

QJsonObject::QJsonObject(std::initializer_list<std::pair<QString, QJsonValue>> args)

Crea un'istanza di QJsonObject inizializzata a partire da un elenco di inizializzazion args. Ad esempio:

QJsonObject object
{
    {"property1", 1},
    {"property2", 2}
};

[noexcept] QJsonObject::QJsonObject(const QJsonObject &other)

Crea una copia di other.

Poiché QJsonObject è implicitamente condiviso, la copia è superficiale fintanto che l'oggetto non viene modificato.

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

Crea un oggetto QJsonObject a partire da other.

[noexcept] QJsonObject::~QJsonObject()

Elimina l'oggetto.

[since 6.10] auto QJsonObject::asKeyValueRange() &&

[since 6.10] auto QJsonObject::asKeyValueRange() &

[since 6.10] auto QJsonObject::asKeyValueRange() const &&

[since 6.10] auto QJsonObject::asKeyValueRange() const &

Restituisce un oggetto intervallo che consente di iterare su questo oggetto come coppie chiave/valore. Ad esempio, questo oggetto intervallo può essere utilizzato in un ciclo `for` basato su intervalli, in combinazione con una dichiarazione di associazione strutturata:

QJsonObject obj{
    { "something", "is" },
    { "in", "this" },
    { "object", 42 },
};

for (auto [key, value]: obj.asKeyValueRange()) {
    qDebug() << key << "->" << value;
   if (key== "object")
        value = "!"; // modifica l'oggetto associato a questa chiave
}
qDebug() << obj["object"]; // QJsonValue(string, "!")

Si noti che il valore ottenuto in questo modo è un riferimento a quello contenuto nell'oggetto. Nello specifico, modificando il valore si modificherà l'oggetto stesso.

Quando si chiama questo metodo su valori r (ad esempio su un oggetto temporaneo creato nell'inizializzatore di un ciclo for a intervallo), l'oggetto verrà catturato in tale intervallo.

Queste funzioni sono state introdotte in Qt 6.10.

Vedi anche QKeyValueIterator.

QJsonObject::iterator QJsonObject::begin()

Restituisce un iteratore in stile STL che punta al primo elemento dell'oggetto.

Vedi anche constBegin() e end().

QJsonObject::const_iterator QJsonObject::begin() const

Questa è una funzione sovraccaricata.

[since 6.12] QJsonObject::const_iterator QJsonObject::cbegin() const

Restituisce un iteratore const in stile STL che punta al primo elemento dell'oggetto.

Questa funzione è stata introdotta in Qt 6.12.

Vedi anche begin() e cend().

[since 6.12] QJsonObject::const_iterator QJsonObject::cend() const

Restituisce un iteratore costante in stile STL che punta all'elemento immaginario successivo all'ultimo elemento dell'oggetto.

Questa funzione è stata introdotta in Qt 6.12.

Vedi anche cbegin() e end().

QJsonObject::const_iterator QJsonObject::constBegin() const

Restituisce un iteratore costante in stile STL che punta al primo elemento dell'oggetto.

Vedi anche begin() e constEnd().

QJsonObject::const_iterator QJsonObject::constEnd() const

Restituisce un iteratore costante in stile STL che punta all'elemento immaginario successivo all'ultimo elemento dell'oggetto.

Vedi anche constBegin() e end().

QJsonObject::const_iterator QJsonObject::constFind(const QString &key) const

Restituisce un iteratore const che punta all'elemento con chiave " key " nella mappa.

Se la mappa non contiene alcun elemento con chiave key, la funzione restituisce constEnd().

QJsonObject::const_iterator QJsonObject::constFind(QLatin1StringView key) const

Questa è una funzione sovraccaricata.

QJsonObject::const_iterator QJsonObject::constFind(QStringView key) const

Questa è una funzione sovraccaricata.

[since 6.10] QJsonObject::const_key_value_iterator QJsonObject::constKeyValueBegin() const

Restituisce un iteratore costante in stile STL che punta alla prima voce dell'oggetto.

Questa funzione è stata introdotta in Qt 6.10.

Vedi anche ` keyValueBegin()`.

[since 6.10] QJsonObject::const_key_value_iterator QJsonObject::constKeyValueEnd() const

Restituisce un iteratore costante in stile STL che punta alla voce immaginaria successiva all'ultima voce dell'oggetto.

Questa funzione è stata introdotta in Qt 6.10.

Vedi anche ` constKeyValueBegin()`.

bool QJsonObject::contains(const QString &key) const

Restituisce true se l’oggetto contiene la chiave key.

Vedi anche insert(), remove() e take().

bool QJsonObject::contains(QLatin1StringView key) const

Questa è una funzione sovraccaricata.

bool QJsonObject::contains(QStringView key) const

Questa è una funzione sovraccaricata.

qsizetype QJsonObject::count() const

È uguale a ` size()`.

bool QJsonObject::empty() const

Questa funzione è fornita per garantire la compatibilità con la STL. È equivalente a ` isEmpty()`, che restituisce ` true ` se l'oggetto è vuoto; in caso contrario, restituisce ` false`.

QJsonObject::iterator QJsonObject::end()

Restituisce un iteratore in stile STL che punta all'elemento immaginario successivo all'ultimo elemento dell'oggetto.

Vedi anche begin() e constEnd().

QJsonObject::const_iterator QJsonObject::end() const

Questa è una funzione sovraccaricata.

QJsonObject::iterator QJsonObject::erase(QJsonObject::iterator it)

Rimuove dalla mappa la coppia (chiave, valore) indicata dall'iteratore it e restituisce un iteratore che punta all'elemento successivo nella mappa.

Vedi anche remove().

QJsonObject::iterator QJsonObject::find(const QString &key)

Restituisce un iteratore che punta all'elemento con chiave key nella mappa.

Se la mappa non contiene alcun elemento con chiave key, la funzione restituisce end().

QJsonObject::iterator QJsonObject::find(QLatin1StringView key)

Questa è una funzione sovraccaricata.

QJsonObject::iterator QJsonObject::find(QStringView key)

Questa è una funzione sovraccaricata.

QJsonObject::const_iterator QJsonObject::find(QLatin1StringView key) const

Questa è una funzione sovraccaricata.

QJsonObject::const_iterator QJsonObject::find(QStringView key) const

Questa è una funzione sovraccaricata.

QJsonObject::const_iterator QJsonObject::find(const QString &key) const

Questa è una funzione sovraccaricata.

[static] QJsonObject QJsonObject::fromVariantHash(const QVariantHash &hash)

Converte l'hash della variante hash in un QJsonObject.

Le chiavi presenti in hash verranno utilizzate come chiavi nell’oggetto JSON, mentre i valori di QVariant verranno convertiti in valori JSON.

Nota: la conversione da QVariant non è completamente priva di perdite. Per ulteriori informazioni, consultare la documentazione relativa a QJsonValue::fromVariant().

Vedi anche fromVariantMap(), toVariantHash() e QJsonValue::fromVariant().

[static] QJsonObject QJsonObject::fromVariantMap(const QVariantMap &map)

Converte la mappa delle varianti map in un QJsonObject.

Le chiavi presenti in map verranno utilizzate come chiavi nell'oggetto JSON, mentre i valori di QVariant verranno convertiti in valori JSON.

Nota: la conversione da QVariant non è completamente priva di perdite. Per ulteriori informazioni, consultare la documentazione relativa a QJsonValue::fromVariant().

Vedi anche fromVariantHash(), toVariantMap() e QJsonValue::fromVariant().

QJsonObject::iterator QJsonObject::insert(const QString &key, const QJsonValue &value)

Inserisce una nuova voce con la chiave key e il valore value.

Se esiste già un elemento con la chiave key, il valore di tale elemento viene sostituito con value.

Restituisce un iteratore che punta alla voce inserita.

Se il valore è QJsonValue::Undefined, la chiave verrà rimossa dall’oggetto. L’iteratore restituito punterà quindi a end().

Vedi anche remove(), take(), QJsonObject::iterator e end().

QJsonObject::iterator QJsonObject::insert(QLatin1StringView key, const QJsonValue &value)

Questa è una funzione sovraccaricata.

QJsonObject::iterator QJsonObject::insert(QStringView key, const QJsonValue &value)

Questa è una funzione sovraccaricata.

bool QJsonObject::isEmpty() const

Restituisce true se l'oggetto è vuoto. È equivalente a size() == 0.

Vedi anche size().

[since 6.10] QJsonObject::key_value_iterator QJsonObject::keyValueBegin()

Restituisce un iteratore in stile STL che punta alla prima voce dell'oggetto.

Questa funzione è stata introdotta in Qt 6.10.

Vedi anche ` keyValueEnd()`.

[since 6.10] QJsonObject::const_key_value_iterator QJsonObject::keyValueBegin() const

Restituisce un iteratore "const" in stile STL che punta alla prima voce dell'oggetto.

Questa funzione è stata introdotta in Qt 6.10.

Vedi anche keyValueEnd().

[since 6.10] QJsonObject::key_value_iterator QJsonObject::keyValueEnd()

Restituisce un iteratore in stile STL che punta alla voce immaginaria successiva all'ultima voce dell'oggetto.

Questa funzione è stata introdotta in Qt 6.10.

Vedi anche ` keyValueBegin()`.

[since 6.10] QJsonObject::const_key_value_iterator QJsonObject::keyValueEnd() const

Restituisce un iteratore costante in stile STL che punta alla voce immaginaria successiva all'ultima voce dell'oggetto.

Questa funzione è stata introdotta in Qt 6.10.

Vedi anche ` keyValueBegin()`.

QStringList QJsonObject::keys() const

Restituisce un elenco di tutte le chiavi presenti in questo oggetto.

L'elenco è ordinato in ordine alfabetico.

qsizetype QJsonObject::length() const

È uguale a ` size()`.

void QJsonObject::remove(const QString &key)

Rimuove l'key dall'oggetto.

Vedi anche insert() e take().

void QJsonObject::remove(QLatin1StringView key)

Questa è una funzione sovraccaricata.

void QJsonObject::remove(QStringView key)

Questa è una funzione sovraccaricata.

qsizetype QJsonObject::size() const

Restituisce il numero di coppie (chiave, valore) memorizzate nell'oggetto.

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

Scambia questo oggetto con other. Questa operazione è molto veloce e non fallisce mai.

QJsonValue QJsonObject::take(const QString &key)

Rimuove l'key dall'oggetto.

Restituisce un oggetto ` QJsonValue ` contenente il valore a cui fa riferimento ` key`. Se ` key ` non era contenuto nell'oggetto, l'oggetto ` QJsonValue ` restituito è ` QJsonValue::Undefined`.

Vedi anche insert(), remove() e QJsonValue.

QJsonValue QJsonObject::take(QLatin1StringView key)

Questa è una funzione sovraccaricata.

QJsonValue QJsonObject::take(QStringView key)

Questa è una funzione sovraccaricata.

QVariantHash QJsonObject::toVariantHash() const

Converte questo oggetto in un oggetto di tipo ` QVariantHash`.

Restituisce l'hash creato.

Vedi anche toVariantMap().

QVariantMap QJsonObject::toVariantMap() const

Converte questo oggetto in un oggetto ` QVariantMap`.

Restituisce la mappa creata.

Vedi anche toVariantHash().

QJsonValue QJsonObject::value(const QString &key) const

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

L'QJsonValue restituito è QJsonValue::Undefined se la chiave non esiste.

Vedi anche ` QJsonValue ` e ` QJsonValue::isUndefined()`.

QJsonValue QJsonObject::value(QLatin1StringView key) const

Questa è una funzione sovraccaricata.

QJsonValue QJsonObject::value(QStringView key) const

Questa è una funzione sovraccaricata.

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

L'operazione "Move" assegna " other " a questo oggetto.

[noexcept] QJsonObject &QJsonObject::operator=(const QJsonObject &other)

Assegna " other " a questo oggetto.

QJsonValueRef QJsonObject::operator[](const QString &key)

Restituisce un riferimento al valore di ` key`. Se nell'oggetto non è presente alcun valore con chiave ` key `, ne viene creato uno con valore ` QJsonValue::Null ` e quindi restituito.

Il valore restituito è di tipo QJsonValueRef, una classe di supporto per QJsonArray e QJsonObject. Quando si ottiene un oggetto di tipo QJsonValueRef, è possibile utilizzarlo come se fosse un riferimento a un QJsonValue. Se gli si assegna un valore, l'assegnazione si applicherà all'elemento contenuto in QJsonArray o QJsonObject da cui è stato ottenuto il riferimento.

Vedi anche value().

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

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

Questo ha lo stesso effetto di ` value()`.

L'QJsonValue restituito è QJsonValue::Undefined se la chiave non esiste.

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

QJsonValueRef QJsonObject::operator[](QLatin1StringView key)

Questa è una funzione sovraccaricata.

QJsonValueRef QJsonObject::operator[](QStringView key)

Questa è una funzione sovraccaricata.

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

Questa è una funzione sovraccaricata.

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

Questa è una funzione sovraccaricata.

Elementi correlati non membri

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

Restituisce ` true ` se l'oggetto ` lhs ` non è uguale a ` rhs`, altrimenti restituisce ` false `.

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

Restituisce ` true ` se l'oggetto ` lhs ` è uguale a ` 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.