QJsonValue¶
The
QJsonValue
class encapsulates a value in JSON. More…
Synopsis¶
Functions¶
def
__eq__
(other)def
__ne__
(other)def
isArray
()def
isBool
()def
isDouble
()def
isNull
()def
isObject
()def
isString
()def
isUndefined
()def
operator[]
(i)def
operator[]
(key)def
swap
(other)def
toArray
()def
toArray
(defaultValue)def
toBool
([defaultValue=false])def
toDouble
([defaultValue=0])def
toInt
([defaultValue=0])def
toObject
()def
toObject
(defaultValue)def
toString
()def
toString
(defaultValue)def
toVariant
()def
type
()
Static functions¶
def
fromVariant
(variant)
Detailed Description¶
A value in JSON can be one of 6 basic types:
JSON is a format to store structured data. It has 6 basic data types:
bool
Bool
double
Double
string
String
array
Array
object
Object
null
Null
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 withisUndefined()
.The type of the value can be queried with
type()
or accessors likeisBool()
,isString()
, and so on. Likewise, the value can be converted to the type stored in it using thetoBool()
,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 forQJsonArray
andQJsonObject
. When you get an object of typeQJsonValueRef
, you can use it as if it were a reference to aQJsonValue
. If you assign to it, the assignment will apply to the element in theQJsonArray
orQJsonObject
from which you got the reference.The following methods return
QJsonValueRef
:
QJsonArray
::operator[](int i)
QJsonObject
::operator[](constQString
& key) constSee also
JSON Support in Qt JSON Save Game Example
-
class
QJsonValue
([arg__1=Null])¶ QJsonValue(b)
QJsonValue(a)
QJsonValue(o)
QJsonValue(other)
QJsonValue(s)
QJsonValue(s)
QJsonValue(n)
QJsonValue(n)
QJsonValue(n)
- param arg__1
Type
- param n
double
- param o
QJsonObject
- param other
- param a
- param b
bool
- param s
unicode
Creates a
QJsonValue
of typetype
.The default is to create a Null value.
Creates a value of type Bool, with value
b
.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.Creates a value of type Double, with value
n
.This is an overloaded function.
Creates a value of type Double, with value
n
.This is an overloaded function.
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.
-
PySide2.QtCore.QJsonValue.
Type
¶ This enum describes the type of the JSON value.
Constant
Description
QJsonValue.Null
A Null value
QJsonValue.Bool
A boolean value. Use
toBool()
to convert to a bool.QJsonValue.Double
A double. Use
toDouble()
to convert to a double.QJsonValue.String
A string. Use
toString()
to convert to aQString
.QJsonValue.Array
An array. Use
toArray()
to convert to aQJsonArray
.QJsonValue.Object
An object. Use
toObject()
to convert to aQJsonObject
.QJsonValue.Undefined
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.
-
static
PySide2.QtCore.QJsonValue.
fromVariant
(variant)¶ - Parameters
variant – object
- Return type
Converts
variant
to aQJsonValue
and returns it.The conversion will convert
QVariant
types as follows:QJsonValue::Map. See
toJsonValue()
for conversion restrictions and the “stringification” of map keys.For all other
QVariant
types a conversion to aQString
will be attempted. If the returned string is empty, a NullQJsonValue
will be stored, otherwise a String value using the returnedQString
.See also
-
PySide2.QtCore.QJsonValue.
isArray
()¶ - Return type
bool
Returns
true
if the value contains an array.See also
-
PySide2.QtCore.QJsonValue.
isBool
()¶ - Return type
bool
Returns
true
if the value contains a boolean.See also
-
PySide2.QtCore.QJsonValue.
isDouble
()¶ - Return type
bool
Returns
true
if the value contains a double.See also
-
PySide2.QtCore.QJsonValue.
isNull
()¶ - Return type
bool
Returns
true
if the value is null.
-
PySide2.QtCore.QJsonValue.
isObject
()¶ - Return type
bool
Returns
true
if the value contains an object.See also
-
PySide2.QtCore.QJsonValue.
isString
()¶ - Return type
bool
Returns
true
if the value contains a string.See also
-
PySide2.QtCore.QJsonValue.
isUndefined
()¶ - Return type
bool
Returns
true
if the value is undefined. This can happen in certain error cases as e.g. accessing a non existing key in aQJsonObject
.
-
PySide2.QtCore.QJsonValue.
__ne__
(other)¶ - Parameters
other –
QJsonValue
- Return type
bool
Returns
true
if the value is not equal toother
.
-
PySide2.QtCore.QJsonValue.
__eq__
(other)¶ - Parameters
other –
QJsonValue
- Return type
bool
Returns
true
if the value is equal toother
.
-
PySide2.QtCore.QJsonValue.operator[](key)
- Parameters
key – unicode
- Return type
-
PySide2.QtCore.QJsonValue.operator[](i)
- Parameters
i –
int
- Return type
Returns a
QJsonValue
representing the value for indexi
.Equivalent to calling
toArray()
.at(i).The returned
QJsonValue
isUndefined
, ifi
is out of bounds, or ifisArray()
is false.See also
-
PySide2.QtCore.QJsonValue.
swap
(other)¶ - Parameters
other –
QJsonValue
Swaps the value
other
with this. This operation is very fast and never fails.
-
PySide2.QtCore.QJsonValue.
toArray
()¶ - Return type
This is an overloaded function.
Converts the value to an array and returns it.
If
type()
is not Array, aQJsonArray()
will be returned.
-
PySide2.QtCore.QJsonValue.
toArray
(defaultValue) - Parameters
defaultValue –
QJsonArray
- Return type
Converts the value to an array and returns it.
If
type()
is not Array, thedefaultValue
will be returned.
-
PySide2.QtCore.QJsonValue.
toBool
([defaultValue=false])¶ - Parameters
defaultValue –
bool
- Return type
bool
Converts the value to a bool and returns it.
If
type()
is not bool, thedefaultValue
will be returned.
-
PySide2.QtCore.QJsonValue.
toDouble
([defaultValue=0])¶ - Parameters
defaultValue –
double
- Return type
double
Converts the value to a double and returns it.
If
type()
is not Double, thedefaultValue
will be returned.
-
PySide2.QtCore.QJsonValue.
toInt
([defaultValue=0])¶ - Parameters
defaultValue –
int
- Return type
int
Converts the value to an int and returns it.
If
type()
is not Double or the value is not a whole number, thedefaultValue
will be returned.
-
PySide2.QtCore.QJsonValue.
toObject
()¶ - Return type
QJsonObject
This is an overloaded function.
Converts the value to an object and returns it.
If
type()
is not Object, theQJsonObject()
will be returned.
-
PySide2.QtCore.QJsonValue.
toObject
(defaultValue) - Parameters
defaultValue –
QJsonObject
- Return type
QJsonObject
Converts the value to an object and returns it.
If
type()
is not Object, thedefaultValue
will be returned.
-
PySide2.QtCore.QJsonValue.
toString
()¶ - Return type
unicode
Converts the value to a
QString
and returns it.If
type()
is not String, a nullQString
will be returned.See also
isNull()
-
PySide2.QtCore.QJsonValue.
toString
(defaultValue) - Parameters
defaultValue – unicode
- Return type
unicode
Converts the value to a
QString
and returns it.If
type()
is not String, thedefaultValue
will be returned.
-
PySide2.QtCore.QJsonValue.
toVariant
()¶ - Return type
object
Converts the value to a
QVariant()
.The
QJsonValue
types will be converted as follows:Null
Nullptr
See also
© 2018 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.