QJsonValue Class
QJsonValue 类封装了一个 JSON 值。更多内容...
| 头文件: | #include <QJsonValue> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
| qmake: | QT += core |
- 所有成员的列表,包括继承的成员
- QJsonValue 是Qt 中 JSON 支持及隐式共享类的一部分。
注意:该类中的所有函数均为可重入的。
QJsonValue 比较
| 类别 | 可比较类型 |
|---|---|
| 相等性 | QJsonValue |
| 相等性 | 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 |
(since 6.10) QAnyStringView | toStringView(QAnyStringView 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 种基本数据类型:
- boolQJsonValue::Bool
- doubleQJsonValue::Double
- stringQJsonValue::String
- arrayQJsonValue::Array
- objectQJsonValue::Object
- nullQJsonValue::Null
一个值可以表示上述任何一种数据类型。此外,QJsonValue 还包含一个特殊标志,用于表示未定义的值。可通过isUndefined() 进行查询。
可以通过 `type()` 或访问器(如 `isBool()`、`isString()` 等)查询值的类型。同样,可以使用 `toBool()`、`toString()` 等方法将值转换为其内部存储的类型。
值在内部是严格类型化的,与QVariant 不同,它不会尝试进行任何隐式类型转换。这意味着,若转换为该值中未存储的类型,将返回一个通过默认构造函数生成的返回值。
QJsonValueRef
QJsonValueRef 是QJsonArray 和QJsonObject 的辅助类。当您获取一个类型为QJsonValueRef 的对象时,可以将其视为对 QJsonValue 的引用。若对其进行赋值,该赋值操作将作用于您获取该引用时所处的QJsonArray 或QJsonObject 中的元素。
以下方法返回QJsonValueRef :
- QJsonArray::operator[](qsizetype i)
- QJsonObject::operator[](constQString & key) const
另请参阅 Qt 中的 JSON 支持以及保存和加载游戏。
成员类型文档
[alias, since 6.9] QJsonValue::JsonFormat
与QJsonDocument::JsonFormat 相同。
该 typedef 是在 Qt 6.9 中引入的。
enum QJsonValue::Type
此枚举描述了 JSON 值的类型。
| 常量 | 值 | 描述 |
|---|---|---|
QJsonValue::Null | 0x0 | 空值 |
QJsonValue::Bool | 0x1 | 布尔值。请使用toBool()将其转换为bool类型。 |
QJsonValue::Double | 0x2 | 数值。使用toDouble()将其转换为double类型,或使用toInteger()将其转换为qint64类型。 |
QJsonValue::String | 0x3 | 字符串。使用toString()将其转换为QString 。 |
QJsonValue::Array | 0x4 | 数组。使用toArray() 将其转换为QJsonArray 。 |
QJsonValue::Object | 0x5 | 一个对象。使用toObject()将其转换为QJsonObject 。 |
QJsonValue::Undefined | 0x80 | 该值未定义。通常在尝试读取数组中的越界值或对象中的不存在的键时,会返回此错误状态。 |
成员函数文档
QJsonValue::QJsonValue(QJsonValue::Type type = Null)
创建一个类型为type 的QJsonValue。
默认情况下将创建一个 Null 值。
QJsonValue::QJsonValue(QLatin1StringView s)
创建一个类型为 String 的值,其中包含由s 显示的 Latin-1 字符串。
QJsonValue::QJsonValue(bool b)
创建一个类型为 Bool 的值,其值为b 。
QJsonValue::QJsonValue(const QJsonArray &a)
创建一个类型为 Array 的值,其值为a 。
QJsonValue::QJsonValue(const QJsonObject &o)
创建一个类型为 Object 的值,其值为o 。
QJsonValue::QJsonValue(const QString &s)
创建一个类型为 String 的值,其值为s 。
QJsonValue::QJsonValue(const char *s)
创建一个类型为 String、值为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)
创建一个类型为 Double 的值,其值为v 。
该值在内部以 64 位整数的形式存储,因此只要通过toInteger() 获取,就能保留其全部精度。但是,如果通过toDouble() 获取其值,除非该值位于 ±2^53 范围内,否则会丢失精度。
这是一个重载函数。
[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 中引入。
另请参阅 QJsonParseError 、isUndefined() 和toJson()。
[static] QJsonValue QJsonValue::fromVariant(const QVariant &variant)
将variant 转换为QJsonValue ,并返回该值。
转换将按以下方式转换QVariant 的类型:
| 源类型 | 目标类型 |
|---|---|
| QJsonValue::Null | |
| QJsonValue::Bool | |
| QJsonValue::Double | |
| QJsonValue::String | |
| QJsonValue::Array | |
| QJsonValue::Object | |
| QJsonValue::String。该转换将使用带有QUrl::FullyEncoded 标志的QUrl::toString() 方法,以确保在解析 URL 时具有最大的兼容性 | |
| QJsonValue::String。自 Qt 5.11 起,生成的字符串将不包含大括号 | |
| 无论 `QCborValue::toJsonValue()` 返回何种类型。 | |
| QJsonValue::Array。有关转换限制,请参阅QCborValue::toJsonValue()。 | |
| QJsonValue::Map。有关转换限制和映射键的“字符串化”,请参阅QCborValue::toJsonValue()。 |
信息丢失及其他类型
QVariant 可能包含超过 JSON 可表示的信息量。如果QVariant 不是上述类型之一,则无法保证转换成功,并且可能会像 UUID 那样在未来的 Qt 版本中发生变化。代码应尽量避免使用上述列表以外的任何其他类型。
如果QVariant::isNull() 返回 true,则会返回一个空的QJsonValue ,或将其插入列表或对象中,无论QVariant 所承载的类型为何。请注意,Qt 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() 进行的转换随时可能发生变化。QVariant 和QJsonValue 未来都可能被扩展以支持更多类型,这将导致该函数执行转换的方式发生改变。
另请参阅 toVariant() 和QCborValue::fromVariant()。
bool QJsonValue::isArray() const
如果该值包含一个数组,则返回true 。
另请参阅 toArray()。
bool QJsonValue::isBool() const
如果该值包含布尔值,则返回true 。
另请参阅 toBool()。
bool QJsonValue::isDouble() const
如果该值包含双精度数值,则返回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()的返回值不是数组,则会返回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
将QJsonValue 转换为UTF-8编码的JSON值,并将其存储在指定的format 中。
该函数于 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()` 的返回值不是字符串,则会返回一个 `null` 的 `QString `。
另请参阅 QString::isNull()。
QString QJsonValue::toString(const QString &defaultValue) const
将该值转换为QString 类型,并返回该值。
如果type()不是字符串,则返回defaultValue 。
另请参阅 ` toStringView()`。
[since 6.10] QAnyStringView QJsonValue::toStringView(QAnyStringView defaultValue = {}) const
返回存储在此QJsonValue 中的字符串值,前提是该字符串属于string 类型。否则,返回defaultValue 。由于QJsonValue 将字符串存储为US-ASCII、UTF-8或UTF-16格式,因此返回的QAnyStringView 可能采用上述任何一种编码。
此函数不会分配内存。返回值的有效期持续到下次对该对象调用非 const 成员函数为止。如果该对象超出作用域,则返回值的有效期持续到下次对父 JSON 对象或数组调用非 const 成员函数为止。
该函数自 Qt 6.10 起引入。
另请参阅 toString()。
QVariant QJsonValue::toVariant() const
将该值转换为QVariant()。
QJsonValue 类型的转换规则如下:
| 常量 | 描述 |
|---|---|
Null | QMetaType::Nullptr |
Bool | QMetaType::Bool |
Double | QMetaType::Double 或QMetaType::LongLong |
String | QString |
Array | QVariantList |
Object | QVariantMap |
Undefined | QVariant() |
另请参阅 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
返回一个QJsonValue ,表示键key 的值。
等同于调用toObject().value(key)。
如果该键不存在,或者isObject() 的返回值为 false,则返回的QJsonValue 即为QJsonValue::Undefined 。
另请参阅 QJsonValue 、QJsonValue::isUndefined() 和QJsonObject 。
const QJsonValue QJsonValue::operator[](qsizetype i) const
返回一个QJsonValue ,表示索引i 对应的值。
等同于调用toArray().at(i)。
若i 超出范围,或isArray() 返回 false,则返回的QJsonValue 为QJsonValue::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 。
© 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.