6.0.0-rc4Source

QJsonObject Class

The QJsonObject class encapsulates a JSON object. More…

Header#include <QJsonObject>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes

List of all members, including inherited members

Public Types

Public Functions

QJsonObject()
QJsonObject(std::initializer_list<std::pair<QString, QJsonValue>> args)
QJsonObject(const QJsonObject &other)
QJsonObject(QJsonObject &&other)
~QJsonObject()
QJsonObject &operator=(const QJsonObject &other)
QJsonObject &operator=(QJsonObject &&other)
voidswap(QJsonObject &other)
qsizetypesize() const
qsizetypecount() const
qsizetypelength() const
boolisEmpty() const
boolempty() const
QVariantMaptoVariantMap() const
QVariantHashtoVariantHash() const
QStringListkeys() const
QJsonValuevalue(const QString &key) const
QJsonValuevalue(QStringView key) const
QJsonValuevalue(QLatin1StringView key) const
QJsonValuevalue(K &&key) const
QJsonValueoperator[](const QString &key) const
QJsonValueoperator[](QStringView key) const
QJsonValueoperator[](QLatin1StringView key) const
QJsonValueoperator[](K &&key) const
QJsonValueRefoperator[](const QString &key)
QJsonValueRefoperator[](QStringView key)
QJsonValueRefoperator[](QLatin1StringView key)
QJsonValueRefoperator[](K &&key)
voidremove(const QString &key)
voidremove(QStringView key)
voidremove(QLatin1StringView key)
voidremove(K &&key)
QJsonValuetake(const QString &key)
QJsonValuetake(QStringView key)
QJsonValuetake(QLatin1StringView key)
QJsonValuetake(K &&key)
boolcontains(const QString &key) const
boolcontains(QStringView key) const
boolcontains(QLatin1StringView key) const
boolcontains(K &&key) const
iteratorbegin()
iteratorend()
const_iteratorbegin() const
const_iteratorend() const
const_iteratorcbegin() const
const_iteratorcend() const
const_iteratorconstBegin() const
const_iteratorconstEnd() const
key_value_iteratorkeyValueBegin()
key_value_iteratorkeyValueEnd()
const_key_value_iteratorkeyValueBegin() const
const_key_value_iteratorkeyValueEnd() const
const_key_value_iteratorconstKeyValueBegin() const
const_key_value_iteratorconstKeyValueEnd() const
autoasKeyValueRange() &
autoasKeyValueRange() const &
reverse_iteratorrbegin()
reverse_iteratorrend()
const_reverse_iteratorrbegin() const
const_reverse_iteratorrend() const
const_reverse_iteratorcrbegin() const
const_reverse_iteratorcrend() const
iteratorinsert(const QString &key, const QJsonValue &value)
iteratorerase(iterator it)
iteratorfind(const QString &key)
iteratorfind(QStringView key)
iteratorfind(QLatin1StringView key)
iteratorfind(K &&key)
const_iteratorfind(const QString &key) const
const_iteratorfind(QStringView key) const
const_iteratorfind(QLatin1StringView key) const
const_iteratorfind(K &&key) const
const_iteratorconstFind(const QString &key) const
const_iteratorconstFind(QStringView key) const
const_iteratorconstFind(QLatin1StringView key) const
const_iteratorconstFind(K &&key) const

Static Public Members

QJsonObjectfromVariantMap(const QVariantMap &map)
QJsonObjectfromVariantHash(const QVariantHash &hash)

Detailed Description

A JSON object is a list of key-value pairs, where the keys are unique strings and the values are QJsonValue. A QJsonObject can be converted to and from a QJsonDocument.

QJsonObject o;
o.insert("name", "Nt");
o["version"] = 6;
for (auto it = o.begin(); it != o.end(); ++it)
    use(it.key(), it.value());

The keys are kept sorted, so keys(), the iterators and the JSON text a document writes all list them in that order, not in the order in which they were inserted. A key is a QString and is compared by UTF-16 code unit.

QJsonObject is implicitly shared: copies are cheap and the data is copied when one of them is modified. It is reentrant, but one object must not be modified while another thread reads it or a copy that shares its data.

Inserting or removing a key invalidates the iterators, because the entries after it move; so does anything else that changes the number of keys. Assigning through an iterator, or through the QJsonValueRef that the operator[] functions return, changes a value in place and does not.

See also QJsonValue, QJsonArray and QJsonDocument.

Differences from Qt

The iterators hold the object and an index instead of a pointer, so a copy made while one is alive is harmless: writes through a non-const iterator go to the object it came from. Assigning an undefined value through operator[] or an iterator stores it, and it is written as null; only insert() removes the key.

Member Type Documentation

typedef QJsonObject::const_key_value_iterator

A const iterator over the members as pairs of a key and a value. The key is a QAnyStringView of the key that the object stores.

This function was introduced in 6.10.

typedef QJsonObject::const_pointer

The type const_iterator::operator->() returns, for STL compatibility.

typedef QJsonObject::const_reference

Alias for QJsonValueConstRef, for STL compatibility.

typedef QJsonObject::const_reverse_iterator

A const reverse iterator over the entries, for STL compatibility.

typedef QJsonObject::ConstIterator

Qt-style synonym for QJsonObject::const_iterator.

typedef QJsonObject::difference_type

Alias for qsizetype, for STL compatibility.

typedef QJsonObject::Iterator

Qt-style synonym for QJsonObject::iterator.

typedef QJsonObject::key_type

Alias for QString, for STL compatibility.

typedef QJsonObject::key_value_iterator

An iterator over the members as pairs of a key and a reference to the value; see QKeyValueIterator. The key is a QAnyStringView of the key that the object stores.

This function was introduced in 6.10.

typedef QJsonObject::mapped_type

Alias for QJsonValue, for STL compatibility.

typedef QJsonObject::pointer

The type iterator::operator->() returns, for STL compatibility.

typedef QJsonObject::reference

Alias for QJsonValueRef, for STL compatibility.

typedef QJsonObject::reverse_iterator

A reverse iterator over the entries, for STL compatibility.

typedef QJsonObject::size_type

Alias for qsizetype, for STL compatibility.

typedef QJsonObject::value_type

Alias for QJsonValue, for STL compatibility.

Member Function Documentation

QJsonObject::QJsonObject()

Creates an empty object.

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

Creates an object with the key-value pairs in args. If a key occurs more than once, the last value is kept.

QJsonObject::QJsonObject(const QJsonObject &other)

Creates a copy of other. The two share their data until one of them is modified.

QJsonObject::QJsonObject(QJsonObject &&other)

Moves other into the new object, which leaves other empty.

QJsonObject::~QJsonObject()

Destroys the object.

auto QJsonObject::asKeyValueRange() &

Returns a range over the members as pairs of key and value, for a range-based for loop with structured bindings:

for (auto [key, value] : object.asKeyValueRange())
    qDebug() << key.toString() << value.toString();

The key of a pair is a QAnyStringView of the key that the object stores: it is valid until a key is inserted or removed, and toString() copies it. The value of a pair from an object that is not const is a reference that can be assigned to.

This function was introduced in 6.10.

auto QJsonObject::asKeyValueRange() const &

This is an overloaded function.

This function was introduced in 6.10.

iterator QJsonObject::begin()

Returns an iterator to the first entry, which has the smallest key.

See also end() and constBegin().

const_iterator QJsonObject::begin() const

This is an overloaded function.

const_iterator QJsonObject::cbegin() const

Returns a const iterator to the first entry.

const_iterator QJsonObject::cend() const

Returns a const iterator to the position after the last entry.

const_iterator QJsonObject::constBegin() const

Returns a const iterator to the first entry.

const_iterator QJsonObject::constEnd() const

Returns a const iterator to the position after the last entry.

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

Returns a const iterator to the entry for key, or constEnd() if the object has no such key.

See also find() and contains().

const_iterator QJsonObject::constFind(QStringView key) const

This is an overloaded function.

const_iterator QJsonObject::constFind(QLatin1StringView key) const

This is an overloaded function.

template <typename K, typename = std::enable_if_t<QtPrivate::IsJsonCString<K>>>const_iterator QJsonObject::constFind(K &&key) const

This is an overloaded function.

Looks key up when it is a C string, such as a string literal.

const_key_value_iterator QJsonObject::constKeyValueBegin() const

Returns a const iterator to the first member, as a pair of key and value.

This function was introduced in 6.10.

const_key_value_iterator QJsonObject::constKeyValueEnd() const

Returns a const iterator just past the last member.

This function was introduced in 6.10.

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

Returns true if the object contains key.

See also value() and keys().

bool QJsonObject::contains(QStringView key) const

This is an overloaded function.

bool QJsonObject::contains(QLatin1StringView key) const

This is an overloaded function.

template <typename K, typename = std::enable_if_t<QtPrivate::IsJsonCString<K>>>bool QJsonObject::contains(K &&key) const

This is an overloaded function.

Looks key up when it is a C string, such as a string literal.

qsizetype QJsonObject::count() const

Same as size().

const_reverse_iterator QJsonObject::crbegin() const

Returns a const reverse iterator to the last entry.

const_reverse_iterator QJsonObject::crend() const

Returns a const reverse iterator to the position before the first entry.

bool QJsonObject::empty() const

Same as isEmpty(), for STL compatibility.

iterator QJsonObject::end()

Returns an iterator to the position after the last entry.

See also begin() and constEnd().

const_iterator QJsonObject::end() const

This is an overloaded function.

iterator QJsonObject::erase(iterator it)

Removes the entry it refers to and returns an iterator to the entry that followed it.

See also remove() and insert().

iterator QJsonObject::find(const QString &key)

Returns an iterator to the entry for key, or end() if the object has no such key.

See also constFind() and contains().

iterator QJsonObject::find(QStringView key)

This is an overloaded function.

iterator QJsonObject::find(QLatin1StringView key)

This is an overloaded function.

template <typename K, typename = std::enable_if_t<QtPrivate::IsJsonCString<K>>>iterator QJsonObject::find(K &&key)

This is an overloaded function.

Looks key up when it is a C string, such as a string literal.

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

This is an overloaded function.

const_iterator QJsonObject::find(QStringView key) const

This is an overloaded function.

const_iterator QJsonObject::find(QLatin1StringView key) const

This is an overloaded function.

template <typename K, typename = std::enable_if_t<QtPrivate::IsJsonCString<K>>>const_iterator QJsonObject::find(K &&key) const

This is an overloaded function.

Looks key up when it is a C string, such as a string literal.

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

Converts hash to an object, converting each value with QJsonValue::fromVariant().

See also toVariantHash(), fromVariantMap() and QJsonValue::fromVariant().

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

Converts map to an object, converting each value with QJsonValue::fromVariant().

See also toVariantMap(), fromVariantHash() and QJsonValue::fromVariant().

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

Inserts value under key and returns an iterator to the new entry. If key exists, its value is replaced. If value is undefined the key is removed instead, and the returned iterator is end().

See also remove().

bool QJsonObject::isEmpty() const

Returns true if the object has no keys.

QStringList QJsonObject::keys() const

Returns the keys of the object in sorted order.

See also contains().

key_value_iterator QJsonObject::keyValueBegin()

Returns an iterator to the first member, as a pair of key and value.

This function was introduced in 6.10.

See also keyValueEnd() and asKeyValueRange().

const_key_value_iterator QJsonObject::keyValueBegin() const

This is an overloaded function.

This function was introduced in 6.10.

key_value_iterator QJsonObject::keyValueEnd()

Returns an iterator just past the last member.

This function was introduced in 6.10.

See also keyValueBegin().

const_key_value_iterator QJsonObject::keyValueEnd() const

This is an overloaded function.

This function was introduced in 6.10.

qsizetype QJsonObject::length() const

Same as size().

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

Assigns other to this object and returns a reference to it.

QJsonObject & QJsonObject::operator=(QJsonObject &&other)

Moves other into this object and returns a reference to it.

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

This is an overloaded function.

Same as value().

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

This is an overloaded function.

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

This is an overloaded function.

template <typename K, typename = std::enable_if_t<QtPrivate::IsJsonCString<K>>>QJsonValue QJsonObject::operator[](K &&key) const

This is an overloaded function.

Looks key up when it is a C string, such as a string literal.

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

Returns a reference to the value for key, which can be assigned to. If the object has no such key it is inserted with a null value first.

See also value() and insert().

QJsonValueRef QJsonObject::operator[](QStringView key)

This is an overloaded function.

QJsonValueRef QJsonObject::operator[](QLatin1StringView key)

This is an overloaded function.

template <typename K, typename = std::enable_if_t<QtPrivate::IsJsonCString<K>>>QJsonValueRef QJsonObject::operator[](K &&key)

This is an overloaded function.

Looks key up when it is a C string, such as a string literal.

reverse_iterator QJsonObject::rbegin()

Returns a reverse iterator to the last entry.

const_reverse_iterator QJsonObject::rbegin() const

This is an overloaded function.

void QJsonObject::remove(const QString &key)

Removes key and its value from the object. Nothing happens if the object has no such key.

See also take() and insert().

void QJsonObject::remove(QStringView key)

This is an overloaded function.

void QJsonObject::remove(QLatin1StringView key)

This is an overloaded function.

template <typename K, typename = std::enable_if_t<QtPrivate::IsJsonCString<K>>>void QJsonObject::remove(K &&key)

This is an overloaded function.

Removes key when it is a C string, such as a string literal.

reverse_iterator QJsonObject::rend()

Returns a reverse iterator to the position before the first entry.

const_reverse_iterator QJsonObject::rend() const

This is an overloaded function.

qsizetype QJsonObject::size() const

Returns the number of key-value pairs in the object.

void QJsonObject::swap(QJsonObject &other)

Swaps this object with other.

QJsonValue QJsonObject::take(const QString &key)

Removes key from the object and returns its value, or returns an undefined value if the object has no such key.

See also remove().

QJsonValue QJsonObject::take(QStringView key)

This is an overloaded function.

QJsonValue QJsonObject::take(QLatin1StringView key)

This is an overloaded function.

template <typename K, typename = std::enable_if_t<QtPrivate::IsJsonCString<K>>>QJsonValue QJsonObject::take(K &&key)

This is an overloaded function.

Takes key when it is a C string, such as a string literal.

QVariantHash QJsonObject::toVariantHash() const

Converts the object to a QVariantHash, converting each value with QJsonValue::toVariant().

See also fromVariantHash(), toVariantMap() and QJsonValue::toVariant().

QVariantMap QJsonObject::toVariantMap() const

Converts the object to a QVariantMap, converting each value with QJsonValue::toVariant().

See also fromVariantMap(), toVariantHash() and QJsonValue::toVariant().

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

Returns the value for key, or an undefined value if the object has no such key.

See also contains().

QJsonValue QJsonObject::value(QStringView key) const

This is an overloaded function.

QJsonValue QJsonObject::value(QLatin1StringView key) const

This is an overloaded function.

template <typename K, typename = std::enable_if_t<QtPrivate::IsJsonCString<K>>>QJsonValue QJsonObject::value(K &&key) const

This is an overloaded function.

Looks key up when it is a C string, such as a string literal.