QJsonDocument 類

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 (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 )
void swap (QJsonDocument & other )
QByteArray toBinaryData () const
QByteArray toJson () const
QByteArray toJson (QJsonDocument::JsonFormat format ) const
QVariant toVariant () const
bool operator!= (const QJsonDocument & other ) const
QJsonDocument & operator= (const QJsonDocument & other )
QJsonDocument & operator= (QJsonDocument && other )
bool operator== (const QJsonDocument & other ) const
const QJsonValue operator[] (const QString & key ) const
const QJsonValue operator[] (QLatin1String key ) const
const QJsonValue operator[] (int i ) const

靜態公共成員

const uint BinaryFormatTag
QJsonDocument fromBinaryData (const QByteArray & data , QJsonDocument::DataValidation validation = Validate)
QJsonDocument fromJson (const QByteArray & json , QJsonParseError * error = nullptr)
QJsonDocument fromRawData (const char * data , int size , QJsonDocument::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 保存遊戲範例 .

成員類型文檔編製

enum QJsonDocument:: DataValidation

此值用於告訴 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.

enum QJsonDocument:: JsonFormat

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}
								

該枚舉在 Qt 5.1 引入或被修改。

成員函數文檔編製

QJsonDocument:: QJsonDocument ()

構造空的無效文檔。

QJsonDocument:: QJsonDocument (const QJsonObject & object )

創建 QJsonDocument from object .

QJsonDocument:: QJsonDocument (const QJsonArray & array )

構造 QJsonDocument from array .

QJsonDocument:: QJsonDocument (const QJsonDocument & other )

創建副本為 other 文檔。

QJsonDocument:: QJsonDocument ( QJsonDocument && other )

移動構造 QJsonDocument from other .

該函數在 Qt 5.10 引入。

QJsonDocument:: ~QJsonDocument ()

刪除文檔。

Binary data set with fromRawData is not freed.

QJsonArray QJsonDocument:: array () const

返迴 QJsonArray 包含在文檔中。

返迴空數組若文檔包含對象。

另請參閱 isArray (), object (),和 setArray ().

[static] QJsonDocument QJsonDocument:: fromBinaryData (const QByteArray & data , QJsonDocument::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 = nullptr)

剖析 json 作為 UTF-8 編碼 JSON 文檔,並創建 QJsonDocument 從它。

返迴有效 (非 null) QJsonDocument 若剖析成功。若它失敗,返迴文檔將為 null,且可選 error 變量將包含有關錯誤的進一步細節。

另請參閱 toJson (), QJsonParseError ,和 isNull ().

[static] QJsonDocument QJsonDocument:: fromRawData (const char * data , int size , QJsonDocument::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 ().

bool QJsonDocument:: isArray () const

返迴 true 若文檔包含數組。

另請參閱 array () 和 isObject ().

bool QJsonDocument:: isEmpty () const

返迴 true 若文檔未包含任何數據。

bool QJsonDocument:: isNull () const

返迴 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.

bool QJsonDocument:: isObject () const

返迴 true 若文檔包含對象。

另請參閱 object () 和 isArray ().

QJsonObject QJsonDocument:: object () const

返迴 QJsonObject 包含在文檔中。

返迴空對象若文檔包含數組。

另請參閱 isObject (), array (),和 setObject ().

const char *QJsonDocument:: rawData ( int * size ) const

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.

void QJsonDocument:: setArray (const QJsonArray & array )

array 作為此文檔的 main 對象。

另請參閱 setObject () 和 array ().

void QJsonDocument:: setObject (const QJsonObject & object )

object 作為此文檔的 main 對象。

另請參閱 setArray () 和 object ().

void QJsonDocument:: swap ( QJsonDocument & other )

交換文檔 other 與此。此操作很快且從不失敗。

該函數在 Qt 5.10 引入。

QByteArray QJsonDocument:: toBinaryData () const

返迴文檔的二進製錶示。

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 ().

QByteArray QJsonDocument:: toJson () const

轉換 QJsonDocument to an indented, UTF-8 encoded JSON document.

另請參閱 fromJson ().

QByteArray QJsonDocument:: toJson ( QJsonDocument::JsonFormat format ) const

轉換 QJsonDocument 成 UTF-8 編碼 JSON 文檔在提供 format .

該函數在 Qt 5.1 引入。

另請參閱 fromJson () 和 JsonFormat .

QVariant QJsonDocument:: toVariant () const

返迴 QVariant 錶示 Json 文檔。

返迴的變體將是 QVariantList 若文檔為 QJsonArray QVariantMap 若文檔為 QJsonObject .

另請參閱 fromVariant () 和 QJsonValue::toVariant ().

bool QJsonDocument:: operator!= (const QJsonDocument & other ) const

返迴 true if other 不等於此文檔

QJsonDocument &QJsonDocument:: operator= (const QJsonDocument & other )

賦值 other 文檔到此 QJsonDocument 。返迴此對象的引用。

QJsonDocument &QJsonDocument:: operator= ( QJsonDocument && other )

移動賦值 other 到此文檔。

該函數在 Qt 5.10 引入。

bool QJsonDocument:: operator== (const QJsonDocument & other ) const

返迴 true other 文檔等於此文檔。

const QJsonValue QJsonDocument:: operator[] (const QString & key ) const

返迴 QJsonValue 錶示值為鍵 key .

相當於調用 object ().value(key).

返迴的 QJsonValue is QJsonValue::Undefined 若鍵不存在,或者若 isObject () 為 false。

該函數在 Qt 5.10 引入。

另請參閱 QJsonValue , QJsonValue::isUndefined (),和 QJsonObject .

const QJsonValue QJsonDocument:: operator[] ( QLatin1String key ) const

這是重載函數。

該函數在 Qt 5.10 引入。

const QJsonValue QJsonDocument:: operator[] ( int i ) const

返迴 QJsonValue representing the value for index i .

相當於調用 array ().at(i).

返迴的 QJsonValue is QJsonValue::Undefined ,若 i 超齣邊界,或者若 isArray () 為 false。

該函數在 Qt 5.10 引入。

另請參閱 QJsonValue , QJsonValue::isUndefined (),和 QJsonArray .