QJsonValue Class
The QJsonValue class encapsulates a value in JSON. More...
| Header: | #include <QJsonValue> |
| qmake: | QT += core |
| Since: | Qt 5.0 |
This class was introduced in Qt 5.0.
Note: All functions in this class are reentrant.
Public Types
| enum | Type { Null, Bool, Double, String, Array, …, Undefined } |
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 QJsonValue::Bool
- double QJsonValue::Double
- string QJsonValue::String
- array QJsonValue::Array
- object QJsonValue::Object
- null QJsonValue::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 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:
- QJsonArray::operator[](int i)
- QJsonObject::operator[](const QString & key) const
See also JSON Support in Qt and JSON Save Game Example.
Member Type Documentation
enum QJsonValue::Type
This enum describes the type of the JSON value.
| Constant | Value | Description |
|---|---|---|
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. |