QJsonDocument 類提供讀寫 JSON 文檔的辦法。 更多...
| 頭: | #include <QJsonDocument> |
| qmake: | QT += core |
| Since: | Qt 5.0 |
該類在 Qt 5.0 引入。
注意: 此類的所有函數 可重入 .
| enum | DataValidation { Validate, BypassValidation } |
| enum | JsonFormat { Indented, Compact } |
| QJsonDocument (QJsonDocument && other ) | |
| QJsonDocument (const QJsonDocument & other ) | |
| QJsonDocument (const QJsonArray & array ) | |
| QJsonDocument (const QJsonObject & object ) | |
| QJsonDocument () | |
| QJsonDocument & | operator= (QJsonDocument && other ) |
| QJsonDocument & | operator= (const 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 () const |
| QByteArray | toJson (QJsonDocument::JsonFormat format ) const |
| QVariant | toVariant () const |
| bool | operator!= (const QJsonDocument & other ) const |
| bool | operator== (const QJsonDocument & other ) const |
| const QJsonValue | operator[] (const QString & key ) const |
| const QJsonValue | operator[] (QStringView key ) const |
| const QJsonValue | operator[] (QLatin1String key ) const |
| const QJsonValue | operator[] (int 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 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 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.
A document can also be created from a stored binary representation using fromBinaryData() or 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}
|
該枚舉在 Qt 5.1 引入或被修改。
移動構造 QJsonDocument 從 other .
該函數在 Qt 5.10 引入。
創建副本為 other 文檔。
構造 QJsonDocument 從 array .
創建 QJsonDocument 從 object .
構造空的無效文檔。
移動賦值 other 到此文檔。
該函數在 Qt 5.10 引入。
賦值 other 文檔到此 QJsonDocument 。返迴此對象的引用。
刪除文檔。
采用 fromRawData 設置的二進製數據不被釋放。
返迴 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 包含任何其它類型除瞭 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 ().
設置 array 作為此文檔的 main 對象。
設置 object 作為此文檔的 main 對象。
交換文檔 other 與此。此操作很快且從不失敗。
該函數在 Qt 5.10 引入。
轉換 QJsonDocument to an indented, UTF-8 encoded JSON document.
另請參閱 fromJson ().
轉換 QJsonDocument 成 UTF-8 編碼 JSON 文檔在提供 format .
該函數在 Qt 5.1 引入。
另請參閱 fromJson () 和 JsonFormat .
返迴 QVariant 錶示 Json 文檔。
返迴的變體將是 QVariantList 若文檔為 QJsonArray 和 QVariantMap 若文檔為 QJsonObject .
另請參閱 fromVariant () 和 QJsonValue::toVariant ().
返迴
true
if
other
不等於此文檔
返迴
true
若
other
文檔等於此文檔。
返迴 QJsonValue 錶示值為鍵 key .
相當於調用 object ().value(key).
返迴的 QJsonValue is QJsonValue::Undefined 若鍵不存在,或者若 isObject () 為 false。
該函數在 Qt 5.10 引入。
另請參閱 QJsonValue , QJsonValue::isUndefined (),和 QJsonObject .
這是重載函數。
該函數在 Qt 5.14 引入。
這是重載函數。
該函數在 Qt 5.10 引入。
返迴 QJsonValue representing the value for index i .
相當於調用 array ().at(i).
返迴的 QJsonValue is QJsonValue::Undefined ,若 i 超齣邊界,或者若 isArray () 為 false。
該函數在 Qt 5.10 引入。
另請參閱 QJsonValue , QJsonValue::isUndefined (),和 QJsonArray .