QJsonObject Class

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

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

该类可等价比较

该类可与QJsonValue 和 QJsonValueConstRef 进行等价比较

注意:该类中的所有函数都是可重入的

公共类型

公共函数

QJsonObject()
QJsonObject(std::initializer_list<std::pair<QString, QJsonValue>> args)
QJsonObject(const QJsonObject &other)
QJsonObject(QJsonObject &&other)
~QJsonObject()
QJsonObject::iterator begin()
QJsonObject::const_iterator begin() 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
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
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 或从 转换为 QJsonObject。您可以通过size(),insert() 和remove() 条目查询(键、值)对的数量,并使用标准 C++ 迭代模式遍历其内容。

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

您可以通过QJsonDocument 将对象转换为基于文本的 JSON。

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

成员类型文档

QJsonObject::ConstIterator

QJsonObject::const_iterator 的 Qt-style 同义词。

QJsonObject::Iterator

QJsonObject::iterator 的 Qt-style 同义词。

QJsonObject::key_type

用于QString 的类型定义。为与 STL 兼容而提供。

QJsonObject::mapped_type

用于QJsonValue 的类型定义。为与 STL 兼容而提供。

QJsonObject::size_type

qsizetype 的类型定义。为与 STL 兼容而提供。

成员函数文档

QJsonObject::QJsonObject()

构造一个空 JSON 对象。

另请参阅 isEmpty()。

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

构造一个 QJsonObject 实例,该实例由args 初始化列表初始化。例如

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

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

创建other 的副本。

由于 QJsonObject 是隐式共享的,因此只要对象不被修改,副本就是浅层的。

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

移动-从other 构建一个 QJsonObject。

[noexcept] QJsonObject::~QJsonObject()

销毁对象。

QJsonObject::iterator QJsonObject::begin()

返回指向对象中第一个项的STL 样式迭代器

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

QJsonObject::const_iterator QJsonObject::begin() const

这是一个重载函数。

QJsonObject::const_iterator QJsonObject::constBegin() const

返回指向对象中第一个项的STL 样式迭代器

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

QJsonObject::const_iterator QJsonObject::constEnd() const

返回一个常STL 样式的迭代器,指向对象中最后一项之后的虚项。

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

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

返回一个常量迭代器,指向 map 中键值为key 的项。

如果 map 中不包含键值为key 的项目,函数返回constEnd() 。

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

这是一个重载函数。

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

这是一个重载函数。

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)

返回指向 map 中键值为key 的项的迭代器。

如果 map 中不包含键值为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()。

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

返回包含key 所引用值的QJsonValue 。如果对象中不包含key ,则返回的QJsonValueQJsonValue::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

返回代表键key 值的QJsonValue

如果键不存在,返回的QJsonValue 将是QJsonValue::Undefined

另请参阅 QJsonValueQJsonValue::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 ,它是QJsonArrayQJsonObject 的辅助类。当您获得QJsonValueRef 类型的对象时,可以将其作为QJsonValue 的引用来使用。如果对其赋值,赋值将应用于您从中获得引用的QJsonArrayQJsonObject 中的元素。

另请参阅 value() 。

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

返回代表键key 值的QJsonValue

其功能与value() 相同。

如果键不存在,返回的QJsonValueQJsonValue::Undefined

另请参阅 value()、QJsonValueQJsonValue::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

© 2025 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.