The QJsonDocument class provides a way to read and write JSON documents. 更多...
| 頭: | #include <QJsonDocument> |
| qmake: | QT += core |
| Since: | Qt 5.0 |
注意: 此類的所有函數 可重入 .
| enum | DataValidation { Validate, BypassValidation } |
| enum | JsonFormat { Indented, Compact } |
| QJsonDocument () | |
| QJsonDocument (const QJsonObject & object ) | |
| QJsonDocument (const QJsonArray & array ) | |
| QJsonDocument (const QJsonDocument & other ) | |
| ~QJsonDocument () | |
| QJsonArray | array () const |
| bool | isArray () const |
| bool | isEmpty () const |
| bool | isNull () const |
| bool | isObject () const |
| QJsonObject | object () const |
| const char * | rawData (int * size ) const |
| void | setArray (const QJsonArray & array ) |
| void | setObject (const QJsonObject & object ) |
| QByteArray | toBinaryData () const |
| QByteArray | toJson (JsonFormat format = Indented) const |
| QVariant | toVariant () const |
| bool | operator!= (const QJsonDocument & other ) const |
| QJsonDocument & | operator= (const QJsonDocument & other ) |
| bool | operator== (const QJsonDocument & other ) const |
| QJsonDocument | fromBinaryData (const QByteArray & data , DataValidation validation = Validate) |
| QJsonDocument | fromJson (const QByteArray & json , QJsonParseError * error = Q_NULLPTR) |
| QJsonDocument | fromRawData (const char * data , int size , DataValidation validation = Validate) |
| QJsonDocument | fromVariant (const QVariant & variant ) |
The QJsonDocument class provides a way to read and write JSON documents.
QJsonDocument is a class that wraps a complete JSON document and can read and write this document both from a UTF-8 encoded text based representation as well as Qt's own binary format.
A JSON document can be converted from its text-based representation to a QJsonDocument 使用 QJsonDocument::fromJson (). toJson () converts it back to text. The parser is very fast and efficient and converts the JSON to the binary representation used by Qt.
Validity of the parsed document can be queried with ! isNull ()
A document can be queried as to whether it contains an array or an object using isArray () 和 isObject (). The array or object contained in the document can be retrieved using array () 或 object () and then read or manipulated.
A document can also be created from a stored binary representation using fromBinaryData () 或 fromRawData ().
另請參閱 在 Qt 中支持 JSON and JSON 保存遊戲範例 .
此值用於告訴 QJsonDocument 是否要驗證二進製數據,當轉換到 QJsonDocument 使用 fromBinaryData () 或 fromRawData ().
| 常量 | 值 | 描述 |
|---|---|---|
QJsonDocument::Validate
|
0
|
Validate the data before using it. This is the default. |
QJsonDocument::BypassValidation
|
1
|
Bypasses data validation. Only use if you received the data from a trusted place and know it's valid, as using of invalid data can crash the application. |
This value defines the format of the JSON byte array produced when converting to a QJsonDocument 使用 toJson ().
| 常量 | 值 | 描述 |
|---|---|---|
QJsonDocument::Indented
|
0
|
定義人性化可讀輸齣如下:
{
"Array"
:
[
true
,
999
,
"string"
]
,
"Key"
:
"Value"
,
"null"
: null
}
|
QJsonDocument::Compact
|
1
|
定義緊湊的輸齣如下:
{"Array":[true,999,"string"],"Key":"Value","null":null}
|
構造空的無效文檔。
創建 QJsonDocument from object .
構造 QJsonDocument from array .
創建副本為 other 文檔。
刪除文檔。
Binary data set with fromRawData is not freed.
返迴 QJsonArray 包含在文檔中。
返迴空數組若文檔包含對象。
另請參閱 isArray (), object (),和 setArray ().
[static]
QJsonDocument
QJsonDocument::
fromBinaryData
(const
QByteArray
&
data
,
DataValidation
validation
= Validate)
創建 QJsonDocument from data .
validation decides whether the data is checked for validity before being used. By default the data is validated. If the data is not valid, the method returns a null document.
另請參閱 toBinaryData (), fromRawData (), isNull (),和 DataValidation .
[static]
QJsonDocument
QJsonDocument::
fromJson
(const
QByteArray
&
json
,
QJsonParseError
*
error
= Q_NULLPTR)
剖析 json 作為 UTF-8 編碼 JSON 文檔,並創建 QJsonDocument 從它。
返迴有效 (非 null) QJsonDocument 若剖析成功。若它失敗,返迴文檔將為 null,且可選 error 變量將包含有關錯誤的進一步細節。
另請參閱 toJson (), QJsonParseError ,和 isNull ().
[static]
QJsonDocument
QJsonDocument::
fromRawData
(const
char
*
data
,
int
size
,
DataValidation
validation
= Validate)
創建 QJsonDocument that uses the first size 字節來自 data . It assumes data contains a binary encoded JSON document. The created document does not take ownership of data and the caller has to guarantee that data will not be deleted or modified as long as any QJsonDocument , QJsonObject or QJsonArray still references the data.
data 必須對齊 4 字節邊界。
validation decides whether the data is checked for validity before being used. By default the data is validated. If the data is not valid, the method returns a null document.
返迴 QJsonDocument 錶示數據。
另請參閱 rawData (), fromBinaryData (), isNull (),和 DataValidation .
[static]
QJsonDocument
QJsonDocument::
fromVariant
(const
QVariant
&
variant
)
創建 QJsonDocument 從 QVariant variant .
若 variant 包含任何其它類型除瞭 QVariantMap , QVariantHash , QVariantList or QStringList ,返迴的文檔無效。
另請參閱 toVariant ().
返迴
true
若文檔包含數組。
返迴
true
若文檔未包含任何數據。
返迴
true
若此文檔為 null。
null 是透過默認構造函數創建文檔。
Documents created from UTF-8 encoded text or the binary format are validated during parsing. If validation fails, the returned document will also be null.
返迴
true
若文檔包含對象。
返迴 QJsonObject 包含在文檔中。
返迴空對象若文檔包含數組。
另請參閱 isObject (), array (),和 setObject ().
Returns the raw binary representation of the data size 將包含返迴數據的大小。
This method is useful to e.g. stream the JSON document in it's binary form to a file.
集 array 作為此文檔的 main 對象。
集 object 作為此文檔的 main 對象。
返迴文檔的二進製錶示。
The binary representation is also the native format used internally in Qt, and is very efficient and fast to convert to and from.
The binary format can be stored on disk and interchanged with other applications or computers. fromBinaryData () can be used to convert it back into a JSON document.
另請參閱 fromBinaryData ().
轉換 QJsonDocument 成 UTF-8 編碼 JSON 文檔在提供 format .
另請參閱 fromJson () 和 JsonFormat .
返迴 QVariant 錶示 Json 文檔。
返迴的變體將是 QVariantList 若文檔為 QJsonArray 和 QVariantMap 若文檔為 QJsonObject .
另請參閱 fromVariant () 和 QJsonValue::toVariant ().
返迴
true
if
other
不等於此文檔
賦值 other 文檔到此 QJsonDocument 。返迴此對象的引用。
返迴
true
若
other
文檔等於此文檔。