QJsonValue Class

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

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

此類 相等可比較 .

此類 相等可比較 with QJsonValueConstRef and QJsonValueRef .

注意: 此類的所有函數 可重入 .

公共類型

(從 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 )
(從 6.3 起) QJsonValue (QJsonArray && a )
(從 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
(從 6.0 起) qint64 toInteger (qint64 defaultValue = 0) const
(從 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
(從 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

靜態公共成員

(從 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 )

詳細描述

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 保存和加載遊戲 .

成員類型文檔編製

[alias, since 6.9] QJsonValue:: JsonFormat

如同 QJsonDocument::JsonFormat .

This typedef was introduced in Qt 6.9.

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 number value. Use toDouble () to convert to a double, or toInteger () to convert to a qint64.
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::Type type = Null)

Creates a QJsonValue of type type .

The default is to create a Null value.

QJsonValue:: QJsonValue ( QLatin1StringView s )

Creates a value of type String, with the Latin-1 string viewed by s .

QJsonValue:: QJsonValue ( bool b )

Creates a value of type Bool, with value b .

QJsonValue:: QJsonValue (const QJsonArray & a )

Creates a value of type Array, with value a .

QJsonValue:: QJsonValue (const QJsonObject & o )

Creates a value of type Object, with value o .

QJsonValue:: QJsonValue (const QString & s )

Creates a value of type String, with value s .

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 當編譯應用程序時。

QJsonValue:: QJsonValue ( double v )

Creates a value of type Double, with value 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 )

Creates a value of type Double, with value v .

這是重載函數。

QJsonValue:: QJsonValue ( qint64 v )

Creates a value of type Double, with value v .

This is stored internally as a 64-bit integer, so retains its full precision, as long as it is retrieved with toInteger (). However, retrieving its value with toDouble () will lose precision unless the value lies between ±2^53.

這是重載函數。

另請參閱 toInteger () 和 toDouble ().

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

創建副本為 other .

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

Move-constructs a QJsonValue from other .

[noexcept] QJsonValue:: ~QJsonValue ()

Destroys the value.

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

剖析 json as a UTF-8 encoded JSON value, and creates a QJsonValue 從它。

Returns a valid QJsonValue if the parsing succeeds. If it fails, the returned value will be undefined , and the optional error 變量將包含有關錯誤的進一步細節。

該函數在 Qt 6.9 引入。

另請參閱 QJsonParseError , isUndefined (),和 toJson ().

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

轉換 variant QJsonValue 並返迴它。

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 () 返迴 true,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 () 會被使用。

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 .

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

Swaps this value with other 。此操作很快且從不失敗。

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 () will be returned.

這是重載函數。

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 將被返迴。

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

Converts the value to an integer and returns it.

type () is not Double or the value is not a whole number representable as qint64, the defaultValue 將被返迴。

該函數在 Qt 6.0 引入。

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

轉換 QJsonValue to a UTF-8 encoded JSON value in the provided format .

該函數在 Qt 6.9 引入。

另請參閱 fromJson () 和 JsonFormat .

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 () will be returned.

這是重載函數。

QString QJsonValue:: toString () const

Converts the value to a QString 並返迴它。

type () is not String, a null QString 將被返迴。

另請參閱 QString::isNull ().

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

Converts the value to a QString 並返迴它。

type () is not String, the defaultValue 將被返迴。

另請參閱 toStringView ().

[since 6.10] QAnyStringView QJsonValue:: toStringView ( QAnyStringView defaultValue = {}) const

Returns the string value stored in this QJsonValue , if it is of the string type. Otherwise, it returns defaultValue 。由於 QJsonValue stores strings in either US-ASCII, UTF-8 or UTF-16, the returned QAnyStringView may be in any of these encodings.

This function does not allocate memory. The return value is valid until the next call to a non-const member function on this object. If this object goes out of scope, the return value is valid until the next call to a non-const member function on the parent JSON object or array.

該函數在 Qt 6.10 引入。

另請參閱 toString ().

QVariant QJsonValue:: toVariant () const

Converts the value to a QVariant ().

The QJsonValue types will be converted as follows:

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

另請參閱 fromVariant ().

QJsonValue::Type QJsonValue:: type () const

Returns the type of the value.

另請參閱 QJsonValue::Type .

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

移動賦值 other to this value.

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

Assigns the value stored in other 到此對象。

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

返迴 QJsonValue 錶示值為鍵 key .

相當於調用 toObject ().value(key).

返迴的 QJsonValue is QJsonValue::Undefined 若鍵不存在,或者若 isObject () 為 false。

另請參閱 QJsonValue , QJsonValue::isUndefined (),和 QJsonObject .

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

返迴 QJsonValue representing the value for index i .

相當於調用 toArray ().at(i).

返迴的 QJsonValue is QJsonValue::Undefined ,若 i 超齣邊界,或者若 isArray () 為 false。

另請參閱 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 )

返迴 truelhs value is not equal to rhs 值, false 否則。

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

返迴 truelhs value is equal to rhs 值, false 否則。