- Qt 6.8
- Qt Core
- C++ Classes
- QJsonArray
- Qt 6.8.3 Reference Documentation

# QJsonArray Class

The QJsonArray class encapsulates a JSON array. More...

Header: | `#include <QJsonArray>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS Core)` `target_link_libraries(mytarget PRIVATE Qt6::Core)`
qmake: | `QT += core`

- List of all members, including inherited members
- QJsonArray is part of JSON Support in Qt and Implicitly Shared Classes.

This class is equality-comparable.

This class is equality-comparable with QJsonValue.

Note: All functions in this class are reentrant.

## Public Types

class | const_iterator
class | iterator
| ConstIterator
| Iterator
| const_pointer
| const_reference
| difference_type
| pointer
| reference
| size_type
| value_type

## Public Functions

| QJsonArray()
| QJsonArray(std::initializer_list<QJsonValue> args)
| QJsonArray(const QJsonArray &other)
| QJsonArray(QJsonArray &&other)
| ~QJsonArray()
void | append(const QJsonValue &value)
QJsonValue | at(qsizetype i) const
QJsonArray::iterator | begin()
QJsonArray::const_iterator | begin() const
QJsonArray::const_iterator | cbegin() const
QJsonArray::const_iterator | cend() const
QJsonArray::const_iterator | constBegin() const
QJsonArray::const_iterator | constEnd() const
bool | contains(const QJsonValue &value) const
qsizetype | count() const
bool | empty() const
QJsonArray::iterator | end()
QJsonArray::const_iterator | end() const
QJsonArray::iterator | erase(QJsonArray::iterator it)
QJsonValue | first() const
QJsonArray::iterator | insert(QJsonArray::iterator before, const QJsonValue &value)
void | insert(qsizetype i, const QJsonValue &value)
bool | isEmpty() const
QJsonValue | last() const
void | pop_back()
void | pop_front()
void | prepend(const QJsonValue &value)
void | push_back(const QJsonValue &value)
void | push_front(const QJsonValue &value)
void | removeAt(qsizetype i)
void | removeFirst()
void | removeLast()
void | replace(qsizetype i, const QJsonValue &value)
qsizetype | size() const
void | swap(QJsonArray &other)
QJsonValue | takeAt(qsizetype i)
QVariantList | toVariantList() const
QJsonArray | operator+(const QJsonValue &value) const
QJsonArray & | operator+=(const QJsonValue &value)
QJsonArray & | operator<<(const QJsonValue &value)
QJsonArray & | operator=(QJsonArray &&other)
QJsonArray & | operator=(const QJsonArray &other)
QJsonValueRef | operator[](qsizetype i)
QJsonValue | operator[](qsizetype i) const

## Static Public Members

QJsonArray | fromStringList(const QStringList &list)
QJsonArray | fromVariantList(const QVariantList &list)

## Related Non-Members

bool | operator!=(const QJsonArray &lhs, const QJsonArray &rhs)
bool | operator==(const QJsonArray &lhs, const QJsonArray &rhs)

## Detailed Description

A JSON array is a list of values. The list can be manipulated by inserting and removing QJsonValue's from the array.

A QJsonArray can be converted to and from a QVariantList. You can query the number of entries with size(), insert(), and removeAt() entries from it and iterate over its content using the standard C++ iterator pattern.

QJsonArray 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 array to and from text based JSON through QJsonDocument.

See also JSON Support in Qt and Saving and Loading a Game.

## Member Type Documentation

### QJsonArray::ConstIterator

Qt-style synonym for QJsonArray::const_iterator.

### QJsonArray::Iterator

Qt-style synonym for QJsonArray::iterator.

### QJsonArray::const_pointer

Typedef for const QJsonValue *. Provided for STL compatibility.

### QJsonArray::const_reference

Typedef for const QJsonValue &. Provided for STL compatibility.

### QJsonArray::difference_type

Typedef for qsizetype. Provided for STL compatibility.

### QJsonArray::pointer

Typedef for QJsonValue *. Provided for STL compatibility.

### QJsonArray::reference

Typedef for QJsonValue &. Provided for STL compatibility.

### QJsonArray::size_type

Typedef for qsizetype. Provided for STL compatibility.

### QJsonArray::value_type

Typedef for QJsonValue. Provided for STL compatibility.

## Member Function Documentation

### QJsonArray::QJsonArray()

Creates an empty array.

### QJsonArray::QJsonArray(std::initializer_list<QJsonValue> args)

Creates an array initialized from args initialization list.

QJsonArray can be constructed in a way similar to JSON notation, for example:

     QJsonArray array = { 1, 2.2, QString() };

### `[noexcept]` QJsonArray::QJsonArray(const QJsonArray &other)

Creates a copy of other.

Since QJsonArray is implicitly shared, the copy is shallow as long as the object doesn't get modified.

### `[noexcept]` QJsonArray::QJsonArray(QJsonArray &&other)

Move-constructs a QJsonArray from other.

### `[noexcept]` QJsonArray::~QJsonArray()

Deletes the array.

### void QJsonArray::append(const QJsonValue &value)

Inserts value at the end of the array.

See also prepend() and insert().

### QJsonValue QJsonArray::at(qsizetype i) const

Returns a QJsonValue representing the value for index i.

The returned QJsonValue is `Undefined`, if i is out of bounds.

### QJsonArray::iterator QJsonArray::begin()

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

See also constBegin() and end().

### QJsonArray::const_iterator QJsonArray::begin() const

This is an overloaded function.

### QJsonArray::const_iterator QJsonArray::cbegin() const

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

See also begin() and cend().

### QJsonArray::const_iterator QJsonArray::cend() const

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

See also cbegin() and end().

### QJsonArray::const_iterator QJsonArray::constBegin() const

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

See also begin() and constEnd().

### QJsonArray::const_iterator QJsonArray::constEnd() const

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

See also constBegin() and end().

### bool QJsonArray::contains(const QJsonValue &value) const

Returns `true` if the array contains an occurrence of value, otherwise `false`.

See also count().

### qsizetype QJsonArray::count() const

Same as size().

See also size().

### bool QJsonArray::empty() const

This function is provided for STL compatibility. It is equivalent to isEmpty() and returns `true` if the array is empty.

### QJsonArray::iterator QJsonArray::end()

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

See also begin() and constEnd().

### QJsonArray::const_iterator QJsonArray::end() const

This is an overloaded function.

### QJsonArray::iterator QJsonArray::erase(QJsonArray::iterator it)

Removes the item pointed to by it, and returns an iterator pointing to the next item.

See also removeAt().

### QJsonValue QJsonArray::first() const

Returns the first value stored in the array.

Same as `at(0)`.

See also at().

### `[static]` QJsonArray QJsonArray::fromStringList(const QStringList &list)

Converts the string list list to a QJsonArray.

The values in list will be converted to JSON values.

See also toVariantList() and QJsonValue::fromVariant().

### `[static]` QJsonArray QJsonArray::fromVariantList(const QVariantList &list)

Converts the variant list list to a QJsonArray.

The QVariant values in list will be converted to JSON values.

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

See also toVariantList() and QJsonValue::fromVariant().

### QJsonArray::iterator QJsonArray::insert(QJsonArray::iterator before, const QJsonValue &value)

Inserts value before the position pointed to by before, and returns an iterator pointing to the newly inserted item.

See also erase() and insert().

### void QJsonArray::insert(qsizetype i, const QJsonValue &value)

Inserts value at index position i in the array. If i is `0`, the value is prepended to the array. If i is size(), the value is appended to the array.

See also append(), prepend(), replace(), and removeAt().

### bool QJsonArray::isEmpty() const

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

See also size().

### QJsonValue QJsonArray::last() const

Returns the last value stored in the array.

Same as `at(size() - 1)`.

See also at().

### void QJsonArray::pop_back()

This function is provided for STL compatibility. It is equivalent to removeLast(). The array must not be empty. If the array can be empty, call isEmpty() before calling this function.

### void QJsonArray::pop_front()

This function is provided for STL compatibility. It is equivalent to removeFirst(). The array must not be empty. If the array can be empty, call isEmpty() before calling this function.

### void QJsonArray::prepend(const QJsonValue &value)

Inserts value at the beginning of the array.

This is the same as `insert(0, value)` and will prepend value to the array.

See also append() and insert().

### void QJsonArray::push_back(const QJsonValue &value)

This function is provided for STL compatibility. It is equivalent to append(value) and will append value to the array.

### void QJsonArray::push_front(const QJsonValue &value)

This function is provided for STL compatibility. It is equivalent to prepend(value) and will prepend value to the array.

### void QJsonArray::removeAt(qsizetype i)

Removes the value at index position i. i must be a valid index position in the array (i.e., `0 <= i < size()`).

See also insert() and replace().

### void QJsonArray::removeFirst()

Removes the first item in the array. Calling this function is equivalent to calling `removeAt(0)`. The array must not be empty. If the array can be empty, call isEmpty() before calling this function.

See also removeAt() and removeLast().

### void QJsonArray::removeLast()

Removes the last item in the array. Calling this function is equivalent to calling `removeAt(size() - 1)`. The array must not be empty. If the array can be empty, call isEmpty() before calling this function.

See also removeAt() and removeFirst().

### void QJsonArray::replace(qsizetype i, const QJsonValue &value)

Replaces the item at index position i with value. i must be a valid index position in the array (i.e., `0 <= i < size()`).

See also operator[]() and removeAt().

### qsizetype QJsonArray::size() const

Returns the number of values stored in the array.

### `[noexcept]` void QJsonArray::swap(QJsonArray &other)

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

### QJsonValue QJsonArray::takeAt(qsizetype i)

Removes the item at index position i and returns it. i must be a valid index position in the array (i.e., `0 <= i < size()`).

If you don't use the return value, removeAt() is more efficient.

See also removeAt().

### QVariantList QJsonArray::toVariantList() const

Converts this object to a QVariantList.

Returns the created map.

### QJsonArray QJsonArray::operator+(const QJsonValue &value) const

Returns an array that contains all the items in this array followed by the provided value.

See also operator+=().

### QJsonArray &QJsonArray::operator+=(const QJsonValue &value)

Appends value to the array, and returns a reference to the array itself.

See also append() and operator<<().

### QJsonArray &QJsonArray::operator<<(const QJsonValue &value)

Appends value to the array, and returns a reference to the array itself.

See also operator+=() and append().

### `[noexcept]` QJsonArray &QJsonArray::operator=(QJsonArray &&other)

Move-assigns other to this array.

### `[noexcept]` QJsonArray &QJsonArray::operator=(const QJsonArray &other)

Assigns other to this array.

### QJsonValueRef QJsonArray::operator[](qsizetype i)

Returns the value at index position i as a modifiable reference. i must be a valid index position in the array (i.e., `0 <= i < size()`).

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 character in the QJsonArray of QJsonObject from which you got the reference.

See also at().

### QJsonValue QJsonArray::operator[](qsizetype i) const

This is an overloaded function.

Same as at().

## Related Non-Members

### `[noexcept]` bool operator!=(const QJsonArray &lhs, const QJsonArray &rhs)

Returns `true` if lhs array is not equal to rhs, `false` otherwise.

### `[noexcept]` bool operator==(const QJsonArray &lhs, const QJsonArray &rhs)

Returns `true` if lhs array is equal to rhs, `false` otherwise.