QJsonDocument 類

QJsonDocument 類提供讀寫 JSON 文檔的辦法。 更多...

頭: #include <QJsonDocument>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

注意: 此類的所有函數 可重入 .

公共類型

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
void setArray (const QJsonArray & array )
void setObject (const QJsonObject & object )
void swap (QJsonDocument & other )
QByteArray toJson (QJsonDocument::JsonFormat format = Indented) 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[] (QStringView key ) const
const QJsonValue operator[] (QLatin1StringView key ) const
const QJsonValue operator[] (qsizetype i ) const

靜態公共成員

QJsonDocument fromJson (const QByteArray & json , QJsonParseError * error = nullptr)
QJsonDocument fromVariant (const QVariant & variant )

詳細描述

QJsonDocument is a class that wraps a complete JSON document and can read this document from, and write it to, a UTF-8 encoded text-based representation.

A JSON document can be converted from its text-based representation to a QJsonDocument using 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.

另請參閱 在 Qt 中支持 JSON and JSON 保存遊戲範例 .

成員類型文檔編製

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}
								

成員函數文檔編製

QJsonDocument:: QJsonDocument ()

構造空的無效文檔。

[explicit] QJsonDocument:: QJsonDocument (const QJsonObject & object )

創建 QJsonDocument 從 object .

[explicit] QJsonDocument:: QJsonDocument (const QJsonArray & array )

構造 QJsonDocument 從 array .

QJsonDocument:: QJsonDocument (const QJsonDocument & other )

創建副本為 other 文檔。

QJsonDocument:: QJsonDocument ( QJsonDocument && other )

移動構造 QJsonDocument 從 other .

QJsonDocument:: ~QJsonDocument ()

刪除文檔。

采用 fromRawData 設置的二進製數據不被釋放。

QJsonArray QJsonDocument:: array () const

返迴 QJsonArray 包含在文檔中。

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

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

[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:: fromVariant (const QVariant & variant )

創建 QJsonDocument QVariant variant .

variant contains any other type than a 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 ().

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 與此。此操作很快且從不失敗。

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

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

另請參閱 fromJson () 和 JsonFormat .

QVariant QJsonDocument:: toVariant () const

返迴 QVariant 錶示 Json 文檔。

The returned variant will be a QVariantList if the document is a QJsonArray and a QVariantMap if the document is a 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 到此文檔。

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。

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

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

這是重載函數。

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

這是重載函數。

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

返迴 QJsonValue representing the value for index i .

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

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

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