CopperSpice API  1.9.1
QJsonObject Class Reference

The QJsonObject class encapsulates a JSON object. More...

Public Typedefs

using const_iterator = QFlatMap< QString, QJsonValue >::const_iterator
 
using iterator = QFlatMap< QString, QJsonValue >::iterator
 
using key_type = QString
 
using mapped_type = QJsonValue
 
using size_type = QFlatMap< QString, QJsonValue >::size_type
 

Public Methods

 QJsonObject ()
 
 QJsonObject (const QJsonObject &other)
 
 QJsonObject (const_iterator iter_begin, const_iterator iter_end)
 
 QJsonObject (QJsonObject &&other)
 
 QJsonObject (std::initializer_list< QPair< QString, QJsonValue >> list)
 
 ~QJsonObject ()
 
iterator begin ()
 
const_iterator begin () const
 
const_iterator constBegin () const
 
const_iterator constEnd () const
 
const_iterator constFind (const QString &key) const
 
bool contains (const QString &key) const
 
int count () const
 
bool empty () const
 
iterator end ()
 
const_iterator end () const
 
iterator erase (const_iterator iter)
 
iterator find (const QString &key)
 
const_iterator find (const QString &key) const
 
iterator insert (const QString &key, QJsonValue value)
 
bool isEmpty () const
 
QStringList keys () const
 
int length () const
 
bool operator!= (const QJsonObject &other) const
 
QJsonObject & operator= (const QJsonObject &other)
 
bool operator== (const QJsonObject &other) const
 
QJsonValueoperator[] (const QString &key)
 
const QJsonValueoperator[] (const QString &key) const
 
void remove (const QString &key)
 
size_type size () const
 
QJsonValue take (const QString &key)
 
QVariantHash toVariantHash () const
 
QVariantMap toVariantMap () const
 
const QJsonValuevalue (const QString &key) const
 

Static Public Methods

static QJsonObject fromVariantHash (const QVariantHash &hash)
 
static QJsonObject fromVariantMap (const QVariantMap &map)
 

Friends

class QJsonValue
 

Detailed Description

The QJsonObject class encapsulates a JSON object. 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 QVariantHash or 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.

The object can be converted to and from a text based JSON format through QJsonDocument.

QJsonObject object1;
object1.insert("tabSpacing", 3);
object1.insert("useSpaces", true);
/* to json */
// create document
QJsonDocument doc1(object1);
// save data to QByteArray
QByteArray data = doc.toJson();
// data can be saved to a file at this point
/* from json */
// load data from QByteArray
// retrieve json object
QJsonObject object2 = doc2.object();
See also
JSON Support

Member Typedef Documentation

Typedef for QJsonValue.

Typedef for int.

Constructor & Destructor Documentation

QJsonObject::QJsonObject ( )

Constructs an empty JSON object.

See also
isEmpty()
QJsonObject::QJsonObject ( const_iterator  iter_begin,
const_iterator  iter_end 
)

Constructs a QJsonArray using an iterator range.

QJsonObject::QJsonObject ( std::initializer_list< QPair< QString, QJsonValue >>  list)

Constructs a QJsonObject initialized from list.

{
{"key_A", 1},
{"key_B", 2}
};
QJsonObject::QJsonObject ( const QJsonObject &  other)

Copy constructs a new QJsonObject from other.

QJsonObject::QJsonObject ( QJsonObject &&  other)

Move constructs a new QJsonObject from other.

QJsonObject::~QJsonObject ( )

Destroys this object.

Method Documentation

iterator QJsonObject::begin ( )

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

See also
constBegin(), end()
const_iterator QJsonObject::begin ( ) const

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

const_iterator QJsonObject::constBegin ( ) const

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

See also
begin(), constEnd()
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
constBegin(), end()
const_iterator QJsonObject::constFind ( const QString key) const

Returns a const iterator pointing to the item with the given key. If the object contains no item with key, returns end.

bool QJsonObject::contains ( const QString key) const

Returns true if the object contains the given key.

See also
insert(), remove(), take()
int QJsonObject::count ( ) const
inline

Equivalent to calling size().

bool QJsonObject::empty ( ) const
inline

Equivalent to isEmpty(), returning true if the object is empty, otherwise returning false.

iterator QJsonObject::end ( )

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

See also
begin(), constEnd()
const_iterator QJsonObject::end ( ) const

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

iterator QJsonObject::erase ( const_iterator  iter)

Removes the (key, value) pair pointed to by the iterator iter, Returns an iterator to the next item in the object.

QJsonObject::const_iterator iter = object.find("someKey");
object.erase(iter);
See also
remove()
iterator QJsonObject::find ( const QString key)

Returns an iterator pointing to the item with the given key. If the object contains no item with key, returns end.

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

Returns a const iterator pointing to the item with the given key. If the object contains no item with key, returns end.

QJsonObject QJsonObject::fromVariantHash ( const QVariantHash hash)
static

Converts the variant 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.

See also
toVariantHash(), QJsonValue::fromVariant()
QJsonObject QJsonObject::fromVariantMap ( const QVariantMap map)
static

Converts the variant 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.

See also
toVariantMap(), QJsonValue::fromVariant()
iterator QJsonObject::insert ( const QString key,
QJsonValue  value 
)

Inserts a new item with the string key and a JsonValue of value. If there is already an item with the key the item's value is replaced with the new value. Returns an iterator pointing to the inserted item.

If the value is QJsonValue::Undefined the key will be removed from the object. The returned iterator points to end().

See also
remove(), take(), QJsonObject::iterator, end()
bool QJsonObject::isEmpty ( ) const

Returns true if the object is empty. Equivalent to calling size() == 0.

See also
empty(), size()
QStringList QJsonObject::keys ( ) const

Returns a list of all keys in this object.

int QJsonObject::length ( ) const
inline

Equivalent to calling size().

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

Returns true if other is not equal to this object.

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

Copy assigns from other and returns a reference to this object.

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

Returns true if other is equal to this object.

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

Returns a reference to the value for key. The return value is of type QJsonValue.

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

Returns a QJsonValue representing the value the given key. Equivalent to calling value(). The returned QJsonValue is QJsonValue::Undefined if the key does not exist.

See also
value(), QJsonValue, QJsonValue::isUndefined()
void QJsonObject::remove ( const QString key)

Removes the (key, value) pair corresponding to the given key.

See also
erase(), insert(), take()
size_type QJsonObject::size ( ) const

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

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
insert(), remove(), QJsonValue
QVariantHash QJsonObject::toVariantHash ( ) const

Converts this object to a QVariantHash. Returns the created hash.

QVariantMap QJsonObject::toVariantMap ( ) const

Converts this object to a QVariantMap. Returns the created map.

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

Returns a QJsonValue representing the value for the given key. The returned QJsonValue is QJsonValue::Undefined if the key does not exist.

See also
QJsonValue, QJsonValue::isUndefined()