本页内容

QJsonObject Class

QJsonObject 类封装了一个 JSON 对象。更多内容...

头文件: #include <QJsonObject>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

注意:该类中的所有函数均为可重入的。

QJsonObject 比较

类别可比较类型
相等性QJsonObject
相等性QJsonValue 以及 QJsonValueConstRef。

公共类型

公共函数

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

静态公共成员

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)

详细说明

JSON 对象是一组键值对,其中键是唯一的字符串,值由 `QJsonValue` 表示。

QJsonObject 可以与QVariantMap 相互转换。您可以使用size()、insert() 和remove() 方法查询其中的 (键, 值) 对的数量,并使用标准的 C++ 迭代器模式遍历其内容。

QJsonObject 是一个隐式共享类,只要其数据未被修改,就会与创建它的文档共享数据。

您可以通过 `QJsonDocument` 将该对象与基于文本的 JSON 相互转换。

另请参阅 Qt 中的 JSON 支持以及游戏的保存与加载。

成员类型文档

QJsonObject::ConstIterator

QJsonObject::const_iterator 的 Qt 风格同义词。

QJsonObject::Iterator

QJsonObject::iterator 的 Qt 风格同义词。

[since 6.10] QJsonObject::const_key_value_iterator

typedef QJsonObject::const_key_value_iterator 为QJsonObject 提供了一个STL风格的迭代器。

QJsonObject::const_key_value_iterator 与QJsonObject::const_iterator 本质上相同,区别在于其 operator*() 返回键值对而非单个值。

该 typedef 于 Qt 6.10 中引入。

另请参阅 QKeyValueIterator 。

QJsonObject::key_type

QString 的类型别名。为兼容 STL 而提供。

[since 6.10] QJsonObject::key_value_iterator

QJsonObject::key_value_iterator 类型别名(typedef)为 `QJsonObject` 提供了一个 STL 风格的迭代器。

QJsonObject::key_value_iterator 与QJsonObject::iterator 本质上相同,唯一的区别在于 operator*() 返回的是键值对,而不是单个值。

该 typedef 于 Qt 6.10 中引入。

另请参阅 QKeyValueIterator 。

QJsonObject::mapped_type

QJsonValue 的 typedef。为兼容 STL 而提供。

QJsonObject::size_type

qsizetype 的 typedef。为兼容 STL 而提供。

成员函数文档

QJsonObject::QJsonObject()

构建一个空的 JSON 对象。

另请参阅 isEmpty()。

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

创建一个从args 初始化列表初始化的QJsonObject实例。例如:

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

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

创建other 的副本。

由于 QJsonObject 是隐式共享的,只要该对象未被修改,则该副本为浅拷贝。

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

从 `other` 创建一个 `QJsonObject`。

[noexcept] QJsonObject::~QJsonObject()

销毁该对象。

[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 &

返回一个范围对象,允许将该对象作为键值对进行迭代。例如,该范围对象可与结构化绑定声明结合使用,在基于范围的 for 循环中:

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

for(auto [key,value]: obj.asKeyValueRange()) {
    qDebug() << key << "->" << value;
   if(key== "object")
        value= "!";// 修改该键对应的对象
}
qDebug() << obj["object"]; // QJsonValue(string, "!")

请注意,通过这种方式获得的值是对象中该值的引用。具体来说,修改该值将直接修改对象本身。

当在右值(例如在范围 for 循环的初始化器中创建的临时对象)上调用此方法时,该对象将被捕获到该范围内。

这些函数在 Qt 6.10 中引入。

另请参阅 QKeyValueIterator 。

QJsonObject::iterator QJsonObject::begin()

返回一个指向该对象中第一个元素的STL 风格迭代器。

另请参阅 constBegin() 和end()。

QJsonObject::const_iterator QJsonObject::begin() const

这是一个重载函数。

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

返回一个指向该对象中第一个项的常量STL 风格迭代器。

该函数在 Qt 6.12 中引入。

另请参阅 begin() 和cend()。

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

返回一个指向对象中最后一个项之后的虚拟项的常量STL 风格迭代器。

该函数在 Qt 6.12 中引入。

另请参阅 cbegin() 和end()。

QJsonObject::const_iterator QJsonObject::constBegin() const

返回一个指向该对象中第一个项的 constSTL 风格迭代器。

另请参阅 begin() 和constEnd()。

QJsonObject::const_iterator QJsonObject::constEnd() const

返回一个指向对象中最后一个项目之后的“虚拟”项目的常量STL 风格迭代器。

另请参阅 constBegin() 和end()。

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

返回一个指向映射中键为key 的项的常量迭代器。

如果映射中不包含键为key 的项,则该函数返回constEnd()。

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

这是一个重载函数。

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

这是一个重载函数。

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

返回一个指向该对象中第一个条目的常量STL 风格迭代器。

该函数在 Qt 6.10 中引入。

另请参阅 keyValueBegin()。

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

返回一个指向对象中最后一个条目之后虚构条目的、STL 风格的const迭代器。

该函数在 Qt 6.10 中引入。

另请参阅 constKeyValueBegin()。

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

如果对象包含键key ,则返回true 。

另请参阅 insert()、remove() 和take()。

bool QJsonObject::contains(QLatin1StringView key) const

这是一个重载函数。

bool QJsonObject::contains(QStringView key) const

这是一个重载函数。

qsizetype QJsonObject::count() const

与size() 相同。

bool QJsonObject::empty() const

提供此函数是为了兼容 STL。它等同于 `isEmpty()`,如果对象为空,则返回 `true `;否则返回 `false`。

QJsonObject::iterator QJsonObject::end()

返回一个STL 风格的迭代器,该迭代器指向对象中最后一个元素之后的虚拟元素。

另请参阅 begin() 和constEnd()。

QJsonObject::const_iterator QJsonObject::end() const

这是一个重载函数。

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

从映射中移除由迭代器it 所指向的 (键, 值) 对,并返回指向映射中下一个元素的迭代器。

另请参阅 remove()。

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

返回一个迭代器,该迭代器指向映射中键为key 的项。

如果映射中不存在键为key 的项,则该函数返回end()。

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

这是一个重载函数。

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

这是一个重载函数。

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

这是一个重载函数。

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

这是一个重载函数。

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

这是一个重载函数。

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

将变体哈希hash 转换为QJsonObject 。

hash 中的键将作为 JSON 对象的键,而QVariant 中的值将被转换为 JSON 值。

注意: 从QVariant 的转换 并非完全无损。更多信息请参阅QJsonValue::fromVariant() 中的文档。

另请参阅 fromVariantMap()、toVariantHash() 和QJsonValue::fromVariant()。

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

将变体映射map 转换为QJsonObject 。

map 中的键将作为 JSON 对象的键,而QVariant 中的值将转换为 JSON 值。

注意: 从QVariant 进行的转换 并非完全无损。更多信息请参阅QJsonValue::fromVariant()中的文档。

另请参阅 fromVariantHash()、toVariantMap() 和QJsonValue::fromVariant()。

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

插入一个新条目,其键为key ,值为value 。

如果已经存在键为key 的条目,则该条目的值将被替换为value 。

返回一个指向已插入项的迭代器。

如果值为QJsonValue::Undefined ,则会导致该键从对象中被移除。此时,返回的迭代器将指向end()。

另请参阅 remove()、take()、QJsonObject::iterator 以及end()。

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

这是一个重载函数。

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

这是一个重载函数。

bool QJsonObject::isEmpty() const

如果对象为空,则返回true 。这与size() == 0 效果相同。

另请参阅 size()。

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

返回一个指向对象中第一个条目的STL 风格迭代器。

该函数在 Qt 6.10 中引入。

另请参阅 keyValueEnd()。

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

返回一个指向对象中第一个条目的常量STL 风格迭代器。

该函数在 Qt 6.10 中引入。

另请参阅 keyValueEnd()。

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

返回一个STL 风格的迭代器,该迭代器指向对象中最后一个条目之后的虚拟条目。

该函数在 Qt 6.10 中引入。

另请参阅 keyValueBegin()。

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

返回一个指向对象中最后一个条目之后虚条目的、STL 风格的const迭代器。

该函数在 Qt 6.10 中引入。

另请参阅 keyValueBegin()。

QStringList QJsonObject::keys() const

返回该对象中所有键的列表。

该列表按字母顺序排序。

qsizetype QJsonObject::length() const

与size() 相同。

void QJsonObject::remove(const QString &key)

从对象中移除key 。

另请参阅 insert() 和take()。

void QJsonObject::remove(QLatin1StringView key)

这是一个重载函数。

void QJsonObject::remove(QStringView key)

这是一个重载函数。

qsizetype QJsonObject::size() const

返回对象中存储的 (键, 值) 对的数量。

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

将该对象与other 互换。此操作速度极快,且绝不会失败。

QJsonValue QJsonObject::take(const QString &key)

从对象中移除key 。

返回一个QJsonValue ,其中包含由key 所引用的值。如果对象中不包含key ,则返回的QJsonValue 为QJsonValue::Undefined 。

另请参阅 insert()、remove(),以及QJsonValue 。

QJsonValue QJsonObject::take(QLatin1StringView key)

这是一个重载函数。

QJsonValue QJsonObject::take(QStringView key)

这是一个重载函数。

QVariantHash QJsonObject::toVariantHash() const

将该对象转换为QVariantHash 。

返回生成的哈希。

另请参阅 toVariantMap()。

QVariantMap QJsonObject::toVariantMap() const

将该对象转换为QVariantMap 。

返回生成的映射。

另请参阅 toVariantHash()。

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

返回一个QJsonValue ,表示键key 的值。

如果该键不存在,则返回的 `QJsonValue ` 为 `QJsonValue::Undefined `。

另请参阅 QJsonValue 和QJsonValue::isUndefined()。

QJsonValue QJsonObject::value(QLatin1StringView key) const

这是一个重载函数。

QJsonValue QJsonObject::value(QStringView key) const

这是一个重载函数。

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

将other 以“移动赋值”方式赋值给该对象。

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

将other 分配给此对象。

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

返回对 `key` 值的引用。如果对象中不存在键为 `key ` 的值,则会创建一个值为 `QJsonValue::Null ` 的键值对,并返回该值。

返回值的类型为QJsonValueRef ,这是QJsonArray 和QJsonObject 的辅助类。当您获取一个QJsonValueRef 类型的对象时,可以将其视为对QJsonValue 的引用。如果您对其进行赋值,该赋值将作用于您获取该引用的源对象(即QJsonArray 或QJsonObject )中的元素。

另请参阅 value()。

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

返回一个QJsonValue ,表示键key 的值。

这与value() 的效果相同。

如果该键不存在,则返回的 `QJsonValue ` 为 `QJsonValue::Undefined `。

另请参阅 value()、QJsonValue 以及QJsonValue::isUndefined()。

QJsonValueRef QJsonObject::operator[](QLatin1StringView key)

这是一个重载函数。

QJsonValueRef QJsonObject::operator[](QStringView key)

这是一个重载函数。

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

这是一个重载函数。

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

这是一个重载函数。

相关的非成员

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

如果lhs 对象不等于rhs ,则返回true ;否则返回false 。

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

如果lhs 对象等于rhs ,则返回true ;否则返回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.