The QJsonValue class encapsulates a value in JSON. 更多...
| 頭: | #include <QJsonValue> |
| qmake: | QT += core |
| Since: | Qt 5.0 |
注意: 此類的所有函數 可重入 .
| enum | Type { Null, Bool, Double, String, ..., Undefined } |
| QJsonValue (QJsonValue::Type type = Null) | |
| QJsonValue (bool b ) | |
| QJsonValue (double n ) | |
| QJsonValue (int n ) | |
| QJsonValue (qint64 n ) | |
| QJsonValue (const QString & s ) | |
| QJsonValue (QLatin1String s ) | |
| QJsonValue (const char * s ) | |
| QJsonValue (const QJsonArray & a ) | |
| QJsonValue (const QJsonObject & o ) | |
| 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 |
| 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 |
| QJsonValue & | operator= (const QJsonValue & other ) |
| QJsonValue & | operator= (QJsonValue && other ) |
| bool | operator== (const QJsonValue & other ) const |
| const QJsonValue | operator[] (const QString & key ) const |
| const QJsonValue | operator[] (QLatin1String key ) const |
| const QJsonValue | operator[] (int i ) const |
| QJsonValue | fromVariant (const QVariant & variant ) |
The QJsonValue class encapsulates a value in JSON.
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 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 保存遊戲範例 .
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 類型 type .
The default is to create a Null value.
Creates a value of type Bool, with value b .
Creates a value of type Double, with value n .
這是重載函數。
Creates a value of type Double, with value n .
這是重載函數。
Creates a value of type Double, with value n . 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.
Creates a value of type String, with value s .
Creates a value of type String, with value 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
當編譯應用程序時。
該函數在 Qt 5.3 引入。
Creates a value of type Array, with value a .
Creates a value of type Object, with value o .
創建副本為 other .
移動構造 QJsonValue from other .
該函數在 Qt 5.10 引入。
Destroys the value.
[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. |
對於所有其它 QVariant types a conversion to a QString will be attempted. If the returned string is empty, a Null QJsonValue will be stored, otherwise a String value using the returned QString .
另請參閱 toVariant ().
返迴
true
if the value contains an array.
另請參閱 toArray ().
返迴
true
if the value contains a boolean.
另請參閱 toBool ().
返迴
true
if the value contains a double.
另請參閱 toDouble ().
返迴
true
if the value is null.
返迴
true
if the value contains an object.
另請參閱 toObject ().
返迴
true
if the value contains a string.
另請參閱 toString ().
返迴
true
if the value is undefined. This can happen in certain error cases as e.g. accessing a non existing key in a
QJsonObject
.
Swaps the value other 與此。此操作很快且從不失敗。
該函數在 Qt 5.10 引入。
Converts the value to an array and returns it.
若 type () is not Array, the defaultValue 將被返迴。
這是重載函數。
Converts the value to an array and returns it.
若 type () is not Array, a QJsonArray() 將被返迴。
Converts the value to a bool and returns it.
若 type () is not bool, the defaultValue 將被返迴。
Converts the value to a double and returns it.
若 type () is not Double, the defaultValue 將被返迴。
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 引入。
Converts the value to an object and returns it.
若 type () is not Object, the defaultValue 將被返迴。
這是重載函數。
Converts the value to an object and returns it.
若 type () is not Object, the QJsonObject() 將被返迴。
Converts the value to a QString 並返迴它。
若 type () is not String, a null QString 將被返迴。
另請參閱 QString::isNull ().
Converts the value to a QString 並返迴它。
若 type () is not String, the defaultValue 將被返迴。
Converts the value to a QVariant() .
The QJsonValue types will be converted as follows:
| 常量 | 描述 |
|---|---|
Null
|
QMetaType::Nullptr |
Bool
|
QMetaType::Bool |
Double
|
QMetaType::Double |
字符串
|
QString |
數組
|
QVariantList |
對象
|
QVariantMap |
未定義
|
QVariant() |
另請參閱 fromVariant ().
Returns the type of the value.
另請參閱 QJsonValue::Type .
返迴
true
if the value is not equal to
other
.
Assigns the value stored in other 到此對象。
移動賦值 other to this value.
該函數在 Qt 5.10 引入。
返迴
true
if the value is equal to
other
.
返迴 QJsonValue 錶示值為鍵 key .
相當於調用 toObject ().value(key).
返迴的 QJsonValue is QJsonValue::Undefined 若鍵不存在,或者若 isObject () 為 false。
該函數在 Qt 5.10 引入。
另請參閱 QJsonValue , QJsonValue::isUndefined (),和 QJsonObject .
這是重載函數。
該函數在 Qt 5.10 引入。
返迴 QJsonValue representing the value for index i .
相當於調用 toArray ().at(i).
返迴的 QJsonValue is QJsonValue::Undefined ,若 i 超齣邊界,或者若 isArray () 為 false。
該函數在 Qt 5.10 引入。
另請參閱 QJsonValue , QJsonValue::isUndefined (),和 QJsonArray .