QJsonValue Class

The QJsonValue class encapsulates a value in JSON. 更多...

头: #include <QJsonValue>
qmake: QT += core
Since: Qt 5.0

该类在 Qt 5.0 引入。

注意: 此类的所有函数 可重入 .

公共类型

enum Type { Null, Bool, Double, String, Array, …, Undefined }

公共函数

QJsonValue (QJsonValue && other )
QJsonValue (const QJsonValue & other )
QJsonValue (const QJsonObject & o )
QJsonValue (const QJsonArray & a )
QJsonValue (const char * s )
QJsonValue (QLatin1String s )
QJsonValue (const QString & s )
QJsonValue (qint64 v )
QJsonValue (int v )
QJsonValue (double v )
QJsonValue (bool b )
QJsonValue (QJsonValue::Type type = Null)
QJsonValue & operator= (QJsonValue && other )
QJsonValue & operator= (const 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
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
bool operator!= (const QJsonValue & other ) const
bool operator== (const QJsonValue & other ) const
const QJsonValue operator[] (const QString & key ) const
const QJsonValue operator[] (QStringView key ) const
const QJsonValue operator[] (QLatin1String key ) const
const QJsonValue operator[] (int i ) const

静态公共成员

QJsonValue fromVariant (const QVariant & variant )

详细描述

A value in JSON can be one of 6 basic types:

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

A value can represent any of the above data types. In addition, QJsonValue has one special flag to represent undefined values. This can be queried with isUndefined ().

The type of the value can be queried with type () or accessors like isBool (), isString (), and so on. Likewise, the value can be converted to the type stored in it using the toBool (), toString () and so on.

Values are strictly typed internally and contrary to QVariant will not attempt to do any implicit type conversions. This implies that converting to a type that is not stored in the value will return a default constructed return value.

QJsonValueRef

QJsonValueRef is 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 element in the QJsonArray or QJsonObject from which you got the reference.

The following methods return QJsonValueRef :

另请参阅 在 Qt 中支持 JSON and JSON 保存游戏范例 .

成员类型文档编制

enum QJsonValue:: Type

This enum describes the type of the JSON value.

常量 描述
QJsonValue::Null 0x0 A Null value
QJsonValue::Bool 0x1 A boolean value. Use toBool () to convert to a bool.
QJsonValue::Double 0x2 A double. Use toDouble () to convert to a double.
QJsonValue::String 0x3 A string. Use toString () to convert to a QString .
QJsonValue::Array 0x4 An array. Use toArray () to convert to a QJsonArray .
QJsonValue::Object 0x5 An object. Use toObject () to convert to a QJsonObject .
QJsonValue::Undefined 0x80 The value is undefined. This is usually returned as an error condition, when trying to read an out of bounds value in an array or a non existent key in an object.

成员函数文档编制

QJsonValue:: QJsonValue ( QJsonValue && other )

Move-constructs a QJsonValue from other .

该函数在 Qt 5.10 引入。

QJsonValue:: QJsonValue (const QJsonValue & other )

Creates a copy of other .

QJsonValue:: QJsonValue (const QJsonObject & o )

Creates a value of type Object, with value o .

QJsonValue:: QJsonValue (const QJsonArray & a )

Creates a value of type Array, with value a .

QJsonValue:: QJsonValue (const char * s )

Creates a value of type String with value s , assuming UTF-8 encoding of the input.

You can disable this constructor by defining QT_NO_CAST_FROM_ASCII when you compile your applications.

该函数在 Qt 5.3 引入。

QJsonValue:: QJsonValue ( QLatin1String s )

Creates a value of type String, with value s .

QJsonValue:: QJsonValue (const QString & s )

Creates a value of type String, with value s .

QJsonValue:: QJsonValue ( qint64 v )

这是重载函数。

Creates a value of type Double, with value v . NOTE: the integer limits for IEEE 754 double precision data is 2^53 (-9007199254740992 to +9007199254740992). If you pass in values outside this range expect a loss of precision to occur.

QJsonValue:: QJsonValue ( int v )

这是重载函数。

Creates a value of type Double, with value v .

QJsonValue:: QJsonValue ( double v )

Creates a value of type Double, with value v .

QJsonValue:: QJsonValue ( bool b )

Creates a value of type Bool, with value b .

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

Creates a QJsonValue of type type .

The default is to create a Null value.

QJsonValue &QJsonValue:: operator= ( QJsonValue && other )

移动赋值 other to this value.

该函数在 Qt 5.10 引入。

QJsonValue &QJsonValue:: operator= (const QJsonValue & other )

Assigns the value stored in other 到此对象。

QJsonValue:: ~QJsonValue ()

Destroys the value.

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

转换 variant QJsonValue and returns it.

The conversion will convert QVariant types as follows:

Source type Destination type
QJsonValue::Null
QJsonValue::Bool
QJsonValue::Double
QJsonValue::String
QJsonValue::Array
QJsonValue::Object
QJsonValue::String . The conversion will use QUrl::toString () with flag QUrl::FullyEncoded , so as to ensure maximum compatibility in parsing the URL
QJsonValue::String . Since Qt 5.11, the resulting string will not include braces
Whichever type QCborValue::toJsonValue () returns.
QJsonValue::Array 。见 QCborValue::toJsonValue () for conversion restrictions.
QJsonValue::Map. See QCborValue::toJsonValue () for conversion restrictions and the "stringification" of map keys.
Loss of information and other types

QVariant can carry more information than is representable in JSON. If the QVariant is not one of the types above, the conversion is not guaranteed and is subject to change in future versions of Qt, as the UUID one did. Code should strive not to use any other types than those listed above.

QVariant::isNull () returns true, a null QJsonValue is returned or inserted into the list or object, regardless of the type carried by QVariant . Note the behavior change in Qt 6.0 affecting QVariant::isNull () also affects this function.

A floating point value that is either an infinity or NaN will be converted to a null JSON value. Since Qt 6.0, QJsonValue can store the full precision of any 64-bit signed integer without loss, but in previous versions values outside the range of ±2^53 may lose precision. Unsigned 64-bit values greater than or equal to 2^63 will either lose precision or alias to negative values, so QMetaType::ULongLong should be avoided.

For other types not listed above, a conversion to string will be attempted, usually but not always by calling QVariant::toString (). If the conversion fails the value is replaced by a null JSON value. Note that QVariant::toString () is also lossy for the majority of types. For example, if the passed QVariant is representing raw byte array data, it is recommended to pre-encode it to Base64 (or another lossless encoding), otherwise a lossy conversion using QString::fromUtf8 () will be used.

Please note that the conversions via QVariant::toString () are subject to change at any time. Both QVariant and QJsonValue may be extended in the future to support more types, which will result in a change in how this function performs conversions.

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

bool QJsonValue:: isArray () const

返回 true if the value contains an array.

另请参阅 toArray ().

bool QJsonValue:: isBool () const

返回 true if the value contains a boolean.

另请参阅 toBool ().

bool QJsonValue:: isDouble () const

返回 true if the value contains a double.

另请参阅 toDouble ().

bool QJsonValue:: isNull () const

返回 true if the value is null.

bool QJsonValue:: isObject () const

返回 true if the value contains an object.

另请参阅 toObject ().

bool QJsonValue:: isString () const

返回 true if the value contains a string.

另请参阅 toString ().

bool QJsonValue:: isUndefined () const

返回 true if the value is undefined. This can happen in certain error cases as e.g. accessing a non existing key in a QJsonObject .

void QJsonValue:: swap ( QJsonValue & other )

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

该函数在 Qt 5.10 引入。

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

Converts the value to an array and returns it.

type () is not Array, the defaultValue 将被返回。

QJsonArray QJsonValue:: toArray () const

这是重载函数。

Converts the value to an array and returns it.

type () is not Array, a QJsonArray() 将被返回。

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

Converts the value to a bool and returns it.

type () is not bool, the defaultValue 将被返回。

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

Converts the value to a double and returns it.

type () is not Double, the defaultValue 将被返回。

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

Converts the value to an int and returns it.

type () is not Double or the value is not a whole number, the defaultValue 将被返回。

该函数在 Qt 5.2 引入。

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

Converts the value to an object and returns it.

type () is not Object, the defaultValue 将被返回。

QJsonObject QJsonValue:: toObject () const

这是重载函数。

Converts the value to an object and returns it.

type () is not Object, the QJsonObject() 将被返回。

QString QJsonValue:: toString () const

Converts the value to a QString and returns it.

type () is not String, a null QString 将被返回。

另请参阅 QString::isNull ().

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

Converts the value to a QString and returns it.

type () is not String, the defaultValue 将被返回。

QVariant QJsonValue:: toVariant () const

Converts the value to a QVariant() .

QJsonValue types will be converted as follows:

常量 描述
Null QMetaType::Nullptr
Bool QMetaType::Bool
Double QMetaType::Double or QMetaType::LongLong
字符串 QString
Array QVariantList
Object QVariantMap
Undefined QVariant()

另请参阅 fromVariant ().

QJsonValue::Type QJsonValue:: type () const

Returns the type of the value.

另请参阅 QJsonValue::Type .

bool QJsonValue:: operator!= (const QJsonValue & other ) const

返回 true if the value is not equal to other .

bool QJsonValue:: operator== (const QJsonValue & other ) const

返回 true if the value is equal to other .

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

返回 QJsonValue 表示值为键 key .

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

返回的 QJsonValue is QJsonValue::Undefined 若键不存在,或者若 isObject () 为 false。

该函数在 Qt 5.10 引入。

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

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

这是重载函数。

该函数在 Qt 5.14 引入。

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

这是重载函数。

该函数在 Qt 5.10 引入。

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

返回 QJsonValue representing the value for index i .

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

返回的 QJsonValue is QJsonValue::Undefined ,若 i 超出边界,或者若 isArray () 为 false。

该函数在 Qt 5.10 引入。

另请参阅 QJsonValue , QJsonValue::isUndefined (),和 QJsonArray .