PySide6.QtCore.QJsonValue¶
- class QJsonValue¶
- The - QJsonValueclass encapsulates a value in JSON. More…- Synopsis¶- Methods¶- def - __init__()
- def - isArray()
- def - isBool()
- def - isDouble()
- def - isNull()
- def - isObject()
- def - isString()
- def - isUndefined()
- def - __ne__()
- def - __eq__()
- def - operator[]()
- def - swap()
- def - toArray()
- def - toBool()
- def - toDouble()
- def - toInt()
- def - toInteger()
- def - toObject()
- def - toString()
- def - toVariant()
- def - type()
 - Static functions¶- def - fromVariant()
 - Note - This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE - 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: - A value can represent any of the above data types. In addition, - QJsonValuehas 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 - QVariantwill 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¶- QJsonValueRefis a helper class for- QJsonArrayand- 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- QJsonArrayor- QJsonObjectfrom which you got the reference.- The following methods return - QJsonValueRef:- QJsonArray::operator[](qsizetype i)
- QJsonObject::operator[](const- QString& key) const
 - See also - JSON Support in Qt Saving and Loading a Game - class 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 number value. Use - toDouble()to convert to a double, or- toInteger()to convert to a qint64.- QJsonValue.String - A string. Use - toString()to convert to a- QString.- QJsonValue.Array - An array. Use - toArray()to convert to a- QJsonArray.- QJsonValue.Object - An object. Use - toObject()to convert to a- QJsonObject.- 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. 
 - Creates a - QJsonValueof type- type.- The default is to create a Null value. - __init__(s)
- Parameters:
- s – - QLatin1String
 
 - __init__(b)
- Parameters:
- b – bool 
 
 - Creates a value of type Bool, with value - b.- __init__(a)
- Parameters:
- a – - QJsonArray
 
 - Creates a value of type Array, with value - a.- __init__(o)
- Parameters:
- o – - QJsonObject
 
 - Creates a value of type Object, with value - o.- __init__(other)
- Parameters:
- other – - QJsonValue
 
 - Creates a copy of - other.- __init__(s)
- Parameters:
- s – str 
 
 - Creates a value of type String, with value - s.- __init__(s)
- Parameters:
- s – str 
 
 - 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_ASCIIwhen you compile your applications.- __init__(n)
- Parameters:
- n – float 
 
 - Creates a value of type Double, with value - v.- __init__(n)
- Parameters:
- n – int 
 
 - This is an overloaded function. - Creates a value of type Double, with value - v.- __init__(v)
- Parameters:
- v – int 
 
 - This is an overloaded function. - 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.- static fromVariant(variant)¶
- Parameters:
- variant – object 
- Return type:
 
 - Converts - variantto a- QJsonValueand returns it.- The conversion will convert - QVarianttypes as follows:- Source type - Destination type - String. The conversion will use- toString()with flag- FullyEncoded, so as to ensure maximum compatibility in parsing the URL- String. Since Qt 5.11, the resulting string will not include braces- Whichever type - toJsonValue()returns.- Array. See- toJsonValue()for conversion restrictions.- QJsonValue::Map. See - toJsonValue()for conversion restrictions and the “stringification” of map keys.- Loss of information and other types¶- QVariantcan carry more information than is representable in JSON. If the- QVariantis 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.- If - isNull()returns true, a null- QJsonValueis returned or inserted into the list or object, regardless of the type carried by- QVariant. Note the behavior change in Qt 6.0 affecting- 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, - QJsonValuecan 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- ULongLongshould be avoided.- For other types not listed above, a conversion to string will be attempted, usually but not always by calling - toString(). If the conversion fails the value is replaced by a null JSON value. Note that- toString()is also lossy for the majority of types. For example, if the passed- QVariantis representing raw byte array data, it is recommended to pre-encode it to Base64 (or another lossless encoding), otherwise a lossy conversion using- fromUtf8()will be used.- Please note that the conversions via - toString()are subject to change at any time. Both- QVariantand- QJsonValuemay be extended in the future to support more types, which will result in a change in how this function performs conversions.- See also - isArray()¶
- Return type:
- bool 
 
 - Returns - trueif the value contains an array.- See also - isBool()¶
- Return type:
- bool 
 
 - Returns - trueif the value contains a boolean.- See also - isDouble()¶
- Return type:
- bool 
 
 - Returns - trueif the value contains a double.- See also - isNull()¶
- Return type:
- bool 
 
 - Returns - trueif the value is null.- isObject()¶
- Return type:
- bool 
 
 - Returns - trueif the value contains an object.- See also - isString()¶
- Return type:
- bool 
 
 - Returns - trueif the value contains a string.- See also - isUndefined()¶
- Return type:
- bool 
 
 - Returns - trueif the value is undefined. This can happen in certain error cases as e.g. accessing a non existing key in a- QJsonObject.- __ne__(rhs)¶
- Parameters:
- rhs – - QJsonArray
- Return type:
- bool 
 
 - __ne__(lhs)
- Parameters:
- lhs – - QJsonObject
- Return type:
- bool 
 
 - __ne__(rhs)
- Parameters:
- rhs – - QJsonObject
- Return type:
- bool 
 
 - __ne__(rhs)
- Parameters:
- rhs – - QJsonValue
- Return type:
- bool 
 
 - Returns - trueif the- lhsvalue is not equal to- rhsvalue,- falseotherwise.- __eq__(rhs)¶
- Parameters:
- rhs – - QJsonArray
- Return type:
- bool 
 
 - __eq__(lhs)
- Parameters:
- lhs – - QJsonObject
- Return type:
- bool 
 
 - __eq__(rhs)
- Parameters:
- rhs – - QJsonObject
- Return type:
- bool 
 
 - __eq__(rhs)
- Parameters:
- rhs – - QJsonValue
- Return type:
- bool 
 
 - Returns - trueif the- lhsvalue is equal to- rhsvalue,- falseotherwise.- operator(key)¶
- Parameters:
- key – - QLatin1String
- Return type:
 
 - operator(key)
- Parameters:
- key – str 
- Return type:
 
 - This is an overloaded function. - operator(key)
- Parameters:
- key – str 
- Return type:
 
 - Returns a - QJsonValuerepresenting the value for the key- key.- Equivalent to calling - toObject().value(key).- The returned - QJsonValueis- Undefinedif the key does not exist, or if- isObject()is false.- See also - QJsonValue- isUndefined()- QJsonObject- operator(i)
- Parameters:
- i – int 
- Return type:
 
 - Returns a - QJsonValuerepresenting the value for index- i.- Equivalent to calling - toArray().at(i).- The returned - QJsonValueis- Undefined, if- iis out of bounds, or if- isArray()is false.- See also - swap(other)¶
- Parameters:
- other – - QJsonValue
 
 - Swaps this value with - other. This operation is very fast and never fails.- toArray()¶
- Return type:
 
 - This is an overloaded function. - Converts the value to an array and returns it. - If - type()is not Array, a- QJsonArray()will be returned.- toArray(defaultValue)
- Parameters:
- defaultValue – - QJsonArray
- Return type:
 
 - Converts the value to an array and returns it. - If - type()is not Array, the- defaultValuewill be returned.- toBool([defaultValue=false])¶
- Parameters:
- defaultValue – bool 
- Return type:
- bool 
 
 - Converts the value to a bool and returns it. - If - type()is not bool, the- defaultValuewill be returned.- toDouble([defaultValue=0])¶
- Parameters:
- defaultValue – float 
- Return type:
- float 
 
 - Converts the value to a double and returns it. - If - type()is not Double, the- defaultValuewill be returned.- 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, the- defaultValuewill be returned.- toInteger([defaultValue=0])¶
- Parameters:
- defaultValue – int 
- Return type:
- int 
 
 - Converts the value to an integer and returns it. - If - type()is not Double or the value is not a whole number representable as qint64, the- defaultValuewill be returned.- toObject()¶
- Return type:
- QJsonObject
 
 - This is an overloaded function. - Converts the value to an object and returns it. - If - type()is not Object, the- QJsonObject()will be returned.- toObject(defaultValue)
- Parameters:
- defaultValue – - QJsonObject
- Return type:
- QJsonObject
 
 - Converts the value to an object and returns it. - If - type()is not Object, the- defaultValuewill be returned.- toString()¶
- Return type:
- str 
 
 - Converts the value to a - QStringand returns it.- If - type()is not String, a null- QStringwill be returned.- See also - isNull()- toString(defaultValue)
- Parameters:
- defaultValue – str 
- Return type:
- str 
 
 - Converts the value to a - QStringand returns it.- If - type()is not String, the- defaultValuewill be returned.- toVariant()¶
- Return type:
- object 
 
 - Converts the value to a - QVariant().- The - QJsonValuetypes will be converted as follows:- Null 
 - See also - Returns the type of the value. - See also