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

QJsonObject Class

The QJsonObject class encapsulates a JSON object.

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+   

QJsonObject Class

  • Header: QJsonObject

  • Since: Qt 5.0

  • CMake:

    find_package(Qt6 REQUIRED COMPONENTS Core)

    target_link_libraries(mytarget PRIVATE Qt6::Core)

  • qmake: QT += core

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

Detailed Description

A JSON object is a list of key value pairs, where the keys are unique strings and the values are represented by a QJsonValue.

A QJsonObject can be converted to and from a QVariantMap. You can query the number of (key, value) pairs with size(), insert(), and remove() entries from it and iterate over its content using the standard C++ iterator pattern.

QJsonObject is an implicitly shared class, and shares the data with the document it has been created from as long as it is not being modified.

You can convert the object to and from text based JSON through QJsonDocument.

See Also

Member Type Documentation

 

QJsonObject::ConstIterator

Qt-style synonym for QJsonObject::const_iterator.

QJsonObject::Iterator

Qt-style synonym for QJsonObject::iterator.

QJsonObject::key_type

Typedef for QString. Provided for STL compatibility.

QJsonObject::mapped_type

Typedef for QJsonValue. Provided for STL compatibility.

QJsonObject::size_type

Typedef for qsizetype. Provided for STL compatibility.

Member Function Documentation

 

QJsonObject::QJsonObject()

Constructs an empty JSON object.

See Also

See also isEmpty()

[since 5.4] QJsonObject::QJsonObject(std::initializer_list<QPair<QString, QJsonValue>> args)

Constructs a QJsonObject instance initialized from args initialization list. For example:

 
Sélectionnez
QJsonObject object
{
    {"property1", 1},
    {"property2", 2}
};

This function was introduced in Qt 5.4.

QJsonObject::QJsonObject(const QJsonObject &other)

Creates a copy of other.

Since QJsonObject is implicitly shared, the copy is shallow as long as the object does not get modified.

[since 5.10] QJsonObject::QJsonObject(QJsonObject &&other)

Move-constructs a QJsonObject from other.

This function was introduced in Qt 5.10.

QJsonObject::~QJsonObject()

Destroys the object.

QJsonObject::iterator QJsonObject::begin()

Returns an STL-style iterator pointing to the first item in the object.

See Also

See also constBegin(), end()

QJsonObject::const_iterator QJsonObject::begin() const

This is an overloaded function.

QJsonObject::const_iterator QJsonObject::constBegin() const

Returns a const STL-style iterator pointing to the first item in the object.

See Also

See also begin(), constEnd()

QJsonObject::const_iterator QJsonObject::constEnd() const

Returns a const STL-style iterator pointing to the imaginary item after the last item in the object.

See Also

See also constBegin(), end()

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

Returns a const iterator pointing to the item with key key in the map.

If the map contains no item with key key, the function returns constEnd().

[since 5.14] QJsonObject::const_iterator QJsonObject::constFind(QStringView key) const

This is an overloaded function.

This function was introduced in Qt 5.14.

[since 5.7] QJsonObject::const_iterator QJsonObject::constFind(QLatin1StringView key) const

This is an overloaded function.

This function was introduced in Qt 5.7.

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

Returns true if the object contains key key.

See Also

See also insert(), remove(), take()

[since 5.14] bool QJsonObject::contains(QStringView key) const

This is an overloaded function.

This function was introduced in Qt 5.14.

[since 5.7] bool QJsonObject::contains(QLatin1StringView key) const

This is an overloaded function.

This function was introduced in Qt 5.7.

qsizetype QJsonObject::count() const

This is an overloaded function.

Same as size().

bool QJsonObject::empty() const

This function is provided for STL compatibility. It is equivalent to isEmpty(), returning true if the object is empty; otherwise returning false.

QJsonObject::iterator QJsonObject::end()

Returns an STL-style iterator pointing to the imaginary item after the last item in the object.

See Also

See also begin(), constEnd()

QJsonObject::const_iterator QJsonObject::end() const

This is an overloaded function.

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

Removes the (key, value) pair pointed to by the iterator it from the map, and returns an iterator to the next item in the map.

See Also

See also remove()

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

Returns an iterator pointing to the item with key key in the map.

If the map contains no item with key key, the function returns end().

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

This is an overloaded function.

[since 5.14] QJsonObject::iterator QJsonObject::find(QStringView key)

This is an overloaded function.

This function was introduced in Qt 5.14.

[since 5.7] QJsonObject::iterator QJsonObject::find(QLatin1StringView key)

This is an overloaded function.

This function was introduced in Qt 5.7.

[since 5.14] QJsonObject::const_iterator QJsonObject::find(QStringView key) const

This is an overloaded function.

This function was introduced in Qt 5.14.

[since 5.7] QJsonObject::const_iterator QJsonObject::find(QLatin1StringView key) const

This is an overloaded function.

This function was introduced in Qt 5.7.

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

Converts the variant hash hash to a QJsonObject.

The keys in hash will be used as the keys in the JSON object, and the QVariant values will be converted to JSON values.

Conversion from QVariant is not completely lossless. Please see the documentation in QJsonValue::fromVariant() for more information.

This function was introduced in Qt 5.5.

See Also

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

Converts the variant map map to a QJsonObject.

The keys in map will be used as the keys in the JSON object, and the QVariant values will be converted to JSON values.

Conversion from QVariant is not completely lossless. Please see the documentation in QJsonValue::fromVariant() for more information.

See Also

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

Inserts a new item with the key key and a value of value.

If there is already an item with the key key, then that item's value is replaced with value.

Returns an iterator pointing to the inserted item.

If the value is QJsonValue::Undefined, it will cause the key to get removed from the object. The returned iterator will then point to end().

See Also

See also remove(), take(), QJsonObject::iterator, end()

[since 5.14] QJsonObject::iterator QJsonObject::insert(QStringView key, const QJsonValue &value)

This is an overloaded function.

This function was introduced in Qt 5.14.

[since 5.14] QJsonObject::iterator QJsonObject::insert(QLatin1StringView key, const QJsonValue &value)

This is an overloaded function.

This function was introduced in Qt 5.14.

bool QJsonObject::isEmpty() const

Returns true if the object is empty. This is the same as size() == 0.

See Also

See also size()

QStringList QJsonObject::keys() const

Returns a list of all keys in this object.

The list is sorted lexographically.

qsizetype QJsonObject::length() const

This is an overloaded function.

Same as size().

void QJsonObject::remove(const QString &key)

Removes key from the object.

See Also

See also insert(), take()

[since 5.14] void QJsonObject::remove(QStringView key)

This is an overloaded function.

This function was introduced in Qt 5.14.

[since 5.14] void QJsonObject::remove(QLatin1StringView key)

This is an overloaded function.

This function was introduced in Qt 5.14.

qsizetype QJsonObject::size() const

Returns the number of (key, value) pairs stored in the object.

[since 5.10] void QJsonObject::swap(QJsonObject &other)

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

This function was introduced in Qt 5.10.

QJsonValue QJsonObject::take(const QString &key)

Removes key from the object.

Returns a QJsonValue containing the value referenced by key. If key was not contained in the object, the returned QJsonValue is QJsonValue::Undefined.

See Also

See also insert(), remove(), QJsonValue

[since 5.14] QJsonValue QJsonObject::take(QStringView key)

This is an overloaded function.

This function was introduced in Qt 5.14.

[since 5.14] QJsonValue QJsonObject::take(QLatin1StringView key)

This is an overloaded function.

This function was introduced in Qt 5.14.

[since 5.5] QVariantHash QJsonObject::toVariantHash() const

Converts this object to a QVariantHash.

Returns the created hash.

This function was introduced in Qt 5.5.

See Also

See also toVariantMap()

QVariantMap QJsonObject::toVariantMap() const

Converts this object to a QVariantMap.

Returns the created map.

See Also

See also toVariantHash()

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

Returns a QJsonValue representing the value for the key key.

The returned QJsonValue is QJsonValue::Undefined if the key does not exist.

See Also

[since 5.14] QJsonValue QJsonObject::value(QStringView key) const

This is an overloaded function.

This function was introduced in Qt 5.14.

[since 5.7] QJsonValue QJsonObject::value(QLatin1StringView key) const

This is an overloaded function.

This function was introduced in Qt 5.7.

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

Returns true if other is not equal to this object.

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

Assigns other to this object.

[since 5.10] QJsonObject &QJsonObject::operator=(QJsonObject &&other)

Move-assigns other to this object.

This function was introduced in Qt 5.10.

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

Returns true if other is equal to this object.

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

Returns a QJsonValue representing the value for the key key.

This does the same as value().

The returned QJsonValue is QJsonValue::Undefined if the key does not exist.

See Also

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

Returns a reference to the value for key. If there is no value with key key in the object, one is created with a QJsonValue::Null value and then returned.

The return value is of type QJsonValueRef, a helper class for QJsonArray and QJsonObject. When you get an object of type QJsonValueRef, you can use it as if it were a reference to a QJsonValue. If you assign to it, the assignment will apply to the element in the QJsonArray or QJsonObject from which you got the reference.

See Also

See also value()

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

This is an overloaded function.

This function was introduced in Qt 5.14.

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

This is an overloaded function.

This function was introduced in Qt 5.7.

[since 5.14] QJsonValueRef QJsonObject::operator[](QStringView key)

This is an overloaded function.

This function was introduced in Qt 5.14.

[since 5.7] QJsonValueRef QJsonObject::operator[](QLatin1StringView key)

This is an overloaded function.

This function was introduced in Qt 5.7.

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