QJsonValue Class

QJsonValue 类封装了一个 JSON 值。更多

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

该类可等价比较

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

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

公共类型

(since 6.9) JsonFormat
enum Type { Null, Bool, Double, String, Array, …, Undefined }

公共函数

QJsonValue(QJsonValue::Type type = Null)
QJsonValue(QLatin1StringView s)
QJsonValue(bool b)
QJsonValue(const QJsonArray &a)
QJsonValue(const QJsonObject &o)
QJsonValue(const QString &s)
QJsonValue(const char *s)
QJsonValue(double v)
(since 6.3) QJsonValue(QJsonArray &&a)
(since 6.3) QJsonValue(QJsonObject &&o)
QJsonValue(int v)
QJsonValue(qint64 v)
QJsonValue(const QJsonValue &other)
QJsonValue(QJsonValue &&other)
~QJsonValue()
bool isArray() const
bool isBool() const
bool isDouble() const
bool isNull() const
bool isObject() const
bool isString() const
bool isUndefined() const
void swap(QJsonValue &other)
QJsonArray toArray(const QJsonArray &defaultValue) const
QJsonArray toArray() const
bool toBool(bool defaultValue = false) const
double toDouble(double defaultValue = 0) const
int toInt(int defaultValue = 0) const
(since 6.0) qint64 toInteger(qint64 defaultValue = 0) const
(since 6.9) QByteArray toJson(QJsonValue::JsonFormat format = JsonFormat::Indented) const
QJsonObject toObject(const QJsonObject &defaultValue) const
QJsonObject toObject() const
QString toString() const
QString toString(const QString &defaultValue) const
QVariant toVariant() const
QJsonValue::Type type() const
QJsonValue &operator=(QJsonValue &&other)
QJsonValue &operator=(const QJsonValue &other)
const QJsonValue operator[](const QString &key) const
const QJsonValue operator[](qsizetype i) const
const QJsonValue operator[](QLatin1StringView key) const
const QJsonValue operator[](QStringView key) const

静态公共成员

(since 6.9) QJsonValue fromJson(QByteArrayView json, QJsonParseError *error = nullptr)
QJsonValue fromVariant(const QVariant &variant)
bool operator!=(const QJsonValue &lhs, const QJsonValue &rhs)
bool operator==(const QJsonValue &lhs, const QJsonValue &rhs)

详细说明

JSON 中的值可以是 6 种基本类型之一:

JSON 是一种存储结构化数据的格式。它有 6 种基本数据类型:

值可以代表上述任何数据类型。此外,QJsonValue 还有一个特殊标记,用于表示未定义的值。可以通过isUndefined() 进行查询。

值的类型可以通过type() 或isBool(),isString() 等访问器查询。同样,也可以使用toBool(),toString() 等访问器将值转换为其中存储的类型。

值在内部是严格类型化的,与QVariant 相反,它不会尝试进行任何隐式类型转换。这意味着,转换到未存储在值中的类型将返回默认构造的返回值。

QJsonValueRef

QJsonValueRef 是 和 的辅助类。当你得到一个 类型的对象时,你可以把它当作 QJsonValue 的引用来使用。如果对其赋值,赋值将应用于 或 中的元素,而您正是从 或 中获取了该引用。QJsonArray QJsonObject QJsonValueRef QJsonArray QJsonObject

以下方法返回QJsonValueRef

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

成员类型文档

[alias, since 6.9] QJsonValue::JsonFormat

QJsonDocument::JsonFormat 相同。

该类型定义在 Qt 6.9 中引入。

enum QJsonValue::Type

该枚举描述 JSON 值的类型。

常量描述
QJsonValue::Null0x0空值
QJsonValue::Bool0x1布尔值。使用toBool() 转换为布尔值。
QJsonValue::Double0x2数值。使用toDouble() 转换为 double,或使用toInteger() 转换为 qint64。
QJsonValue::String0x3字符串。使用toString() 可转换为QString
QJsonValue::Array0x4数组。使用toArray() 可转换为QJsonArray
QJsonValue::Object0x5对象。使用toObject() 可转换为QJsonObject
QJsonValue::Undefined0x80值是未定义的。当试图读取数组中的越界值或对象中不存在的键时,通常会作为错误条件返回。

成员函数文档

QJsonValue::QJsonValue(QJsonValue::Type type = Null)

创建type 类型的 QJsonValue。

默认创建一个空值。

QJsonValue::QJsonValue(QLatin1StringView s)

创建一个字符串类型的值,其中的 Latin-1 字符串由s 查看。

QJsonValue::QJsonValue(bool b)

创建一个 Bool 类型的值,值为b

QJsonValue::QJsonValue(const QJsonArray &a)

创建数组类型的值,值为a

QJsonValue::QJsonValue(const QJsonObject &o)

创建对象类型的值,值为o

QJsonValue::QJsonValue(const QString &s)

创建字符串类型的值,值为s

QJsonValue::QJsonValue(const char *s)

创建一个字符串类型的值,其值为s ,假设输入为 UTF-8 编码。

您可以在编译应用程序时通过定义QT_NO_CAST_FROM_ASCII 禁用该构造函数。

QJsonValue::QJsonValue(double v)

创建一个 Double 类型的值,值为v

[noexcept, since 6.3] QJsonValue::QJsonValue(QJsonArray &&a)

这是一个重载函数。

该函数在 Qt 6.3 中引入。

[noexcept, since 6.3] QJsonValue::QJsonValue(QJsonObject &&o)

这是一个重载函数。

该函数在 Qt 6.3 中引入。

QJsonValue::QJsonValue(int v)

这是一个重载函数。

创建一个 Double 类型的值,值为v

QJsonValue::QJsonValue(qint64 v)

这是一个重载函数。

创建值为v 的 Double 类型值。注意:IEEE 754 双精度数据的整数限制为 2^53(-9007199254740992 至 +9007199254740992)。如果您输入的值超出了这个范围,就会出现精度损失。

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

创建other 的副本。

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

移动-从other 构建一个 QJsonValue。

[noexcept] QJsonValue::~QJsonValue()

破坏值。

[static, since 6.9] QJsonValue QJsonValue::fromJson(QByteArrayView json, QJsonParseError *error = nullptr)

json 解析为 UTF-8 编码的 JSON 值,并从中创建QJsonValue

如果解析成功,则返回有效的QJsonValue 。如果解析失败,返回值将是undefined ,可选的error 变量将包含有关错误的更多详细信息。

此函数在 Qt 6.9 中引入。

另请参阅 QJsonParseErrorisUndefined() 和toJson()。

[static] QJsonValue QJsonValue::fromVariant(const QVariant &variant)

variant 转换为QJsonValue 并返回。

转换将按如下方式转换QVariant 类型:

信息丢失和其他类型

QVariant QJsonValue::Map 可以携带比 JSON 所能表示的更多的信息。如果 不是上述类型之一,则不能保证进行转换,并且在 Qt XML 的未来版本中可能会发生变化,就像 UUID 的变化一样。代码应尽量不使用上述类型以外的其他类型。QVariant

如果QVariant::isNull() 返回 true,则返回一个空QJsonValue 或插入到列表或对象中,而不管QVariant 所携带的类型是什么。请注意 Qt XML 6.0 中影响QVariant::isNull() 的行为变化也会影响此函数。

无穷大或 NaN 的浮点数值将被转换为 JSON 空值。自 Qt 6.0 起,QJsonValue 可以无损地存储任何 64 位有符号整数的全部精度,但在以前的版本中,超出 ±2^53 范围的值可能会丢失精度。大于或等于 2^63 的 64 位无符号数值要么会丢失精度,要么会别名为负值,因此应避免使用QMetaType::ULongLong

对于上面未列出的其他类型,将尝试转换为字符串,通常但不总是调用QVariant::toString() 。如果转换失败,该值将被空 JSON 值取代。请注意,对于大多数类型,QVariant::toString() 也是有损的。例如,如果传递的QVariant 表示原始字节数组数据,建议将其预编码为Base64(或其他无损编码),否则将使用QString::fromUtf8() 进行有损转换。

请注意,通过QVariant::toString() 进行的转换可能会随时更改。QVariantQJsonValue 今后可能会扩展以支持更多类型,这将导致该函数执行转换的方式发生变化。

另请参阅 toVariant() 和QCborValue::fromVariant()。

bool QJsonValue::isArray() const

如果值包含数组,则返回true

另请参见 toArray()。

bool QJsonValue::isBool() const

如果值包含布尔值,则返回true

另请参见 toBool()。

bool QJsonValue::isDouble() const

如果数值包含 double,则返回true

另请参见 toDouble().

bool QJsonValue::isNull() const

如果值为空,则返回true

bool QJsonValue::isObject() const

如果值包含一个对象,则返回true

另请参见 toObject()。

bool QJsonValue::isString() const

如果值包含字符串,则返回true

另请参阅 toString()。

bool QJsonValue::isUndefined() const

如果值未定义,则返回true 。这可能发生在某些错误情况下,例如访问QJsonObject 中不存在的键。

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

将此值与other 互换。该操作速度非常快,从未出现过故障。

QJsonArray QJsonValue::toArray(const QJsonArray &defaultValue) const

将数值转换为数组并返回。

如果type() 不是数组,则会返回defaultValue

QJsonArray QJsonValue::toArray() const

这是一个重载函数。

将值转换为数组并返回。

如果type() 不是 Array,则将返回QJsonArray()。

bool QJsonValue::toBool(bool defaultValue = false) const

将值转换为 bool 并返回。

如果type() 不是 bool,则会返回defaultValue

double QJsonValue::toDouble(double defaultValue = 0) const

将数值转换为 double 并返回。

如果type() 不是 Double,则会返回defaultValue

int QJsonValue::toInt(int defaultValue = 0) const

将数值转换为 int 并返回。

如果type() 不是 Double 或值不是整数,则将返回defaultValue

[since 6.0] qint64 QJsonValue::toInteger(qint64 defaultValue = 0) const

将数值转换为整数并返回。

如果type() 不是 Double 或值不是可表示为 qint64 的整数,则将返回defaultValue

此函数在 Qt 6.0 中引入。

[since 6.9] QByteArray QJsonValue::toJson(QJsonValue::JsonFormat format = JsonFormat::Indented) const

在提供的format 中将QJsonValue 转换为 UTF-8 编码的 JSON 值。

此函数在 Qt 6.9 中引入。

另请参阅 fromJson() 和JsonFormat

QJsonObject QJsonValue::toObject(const QJsonObject &defaultValue) const

将数值转换为对象并返回。

如果type() 不是 Object,则将返回defaultValue

QJsonObject QJsonValue::toObject() const

这是一个重载函数。

将数值转换为对象并返回。

如果type() 不是 Object,则将返回QJsonObject()。

QString QJsonValue::toString() const

将值转换为QString 并返回。

如果type() 不是字符串,则将返回空QString

另请参阅 QString::isNull()。

QString QJsonValue::toString(const QString &defaultValue) const

将值转换为QString 并返回。

如果type() 不是字符串,则将返回defaultValue

QVariant QJsonValue::toVariant() const

将数值转换为QVariant() 。

QJsonValue 类型的转换如下:

另请参见 fromVariant()。

QJsonValue::Type QJsonValue::type() const

返回值的类型。

另请参见 QJsonValue::Type

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

移动--将other 指定为该值。

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

other 中存储的值赋值给此对象。

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

返回代表键key 值的QJsonValue

相当于调用toObject().value(key) 。

如果键不存在,或isObject() 为 false,则返回的QJsonValueQJsonValue::Undefined

另请参阅 QJsonValue,QJsonValue::isUndefined() 和QJsonObject

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

返回代表索引i 值的QJsonValue

相当于调用toArray().at(i) 。

如果i 越界,或isArray() 为 false,则返回的QJsonValueQJsonValue::Undefined

另请参见 QJsonValue,QJsonValue::isUndefined() 和QJsonArray

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

这是一个重载函数。

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

这是一个重载函数。

相关非成员

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

如果lhs 值不等于rhs 值,则返回true ,否则返回false

[noexcept] bool operator==(const QJsonValue &lhs, const QJsonValue &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.