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 QJsonArray & array ) | |
| QJsonDocument (const QJsonObject & object ) | |
| 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 |
| QJsonDocument & | operator= (QJsonDocument && other ) |
| QJsonDocument & | operator= (const QJsonDocument & other ) |
| const QJsonValue | operator[] (const QString & key ) const |
| const QJsonValue | operator[] (qsizetype i ) const |
| const QJsonValue | operator[] (QLatin1StringView key ) const |
| const QJsonValue | operator[] (QStringView key ) const |
| QJsonDocument | fromJson (const QByteArray & json , QJsonParseError * error = nullptr) |
| QJsonDocument | fromVariant (const QVariant & variant ) |
| bool | operator!= (const QJsonDocument & lhs , const QJsonDocument & rhs ) |
| bool | operator== (const QJsonDocument & lhs , const QJsonDocument & rhs ) |
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 保存和加載遊戲 .
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}
|
構造空的無效文檔。
[explicit]
QJsonDocument::
QJsonDocument
(const
QJsonArray
&
array
)
構造 QJsonDocument 從 array .
[explicit]
QJsonDocument::
QJsonDocument
(const
QJsonObject
&
object
)
創建 QJsonDocument 從 object .
創建副本為 other 文檔。
[noexcept]
QJsonDocument::
QJsonDocument
(
QJsonDocument
&&
other
)
移動構造 QJsonDocument 從 other .
[noexcept]
QJsonDocument::
~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 對象。
[noexcept]
void
QJsonDocument::
swap
(
QJsonDocument
&
other
)
Swaps this document with other 。此操作很快且從不失敗。
轉換 QJsonDocument 成 UTF-8 編碼 JSON 文檔在提供 format .
另請參閱 fromJson () 和 JsonFormat .
返迴 QVariant 錶示 Json 文檔。
返迴的變體將是 QVariantList 若文檔為 QJsonArray 和 QVariantMap 若文檔為 QJsonObject .
另請參閱 fromVariant () 和 QJsonValue::toVariant ().
[noexcept]
QJsonDocument
&QJsonDocument::
operator=
(
QJsonDocument
&&
other
)
移動賦值 other 到此文檔。
賦值 other 文檔到此 QJsonDocument 。返迴此對象的引用。
返迴 QJsonValue 錶示值為鍵 key .
相當於調用 object ().value(key).
返迴的 QJsonValue is QJsonValue::Undefined 若鍵不存在,或者若 isObject () 為 false。
另請參閱 QJsonValue , QJsonValue::isUndefined (),和 QJsonObject .
返迴 QJsonValue representing the value for index i .
相當於調用 array ().at(i).
返迴的 QJsonValue is QJsonValue::Undefined ,若 i 超齣邊界,或者若 isArray () 為 false。
另請參閱 QJsonValue , QJsonValue::isUndefined (),和 QJsonArray .
這是重載函數。
這是重載函數。
[noexcept]
bool
operator!=
(const
QJsonDocument
&
lhs
, const
QJsonDocument
&
rhs
)
返迴
true
若
lhs
document is not equal to
rhs
document,
false
否則。
[noexcept]
bool
operator==
(const
QJsonDocument
&
lhs
, const
QJsonDocument
&
rhs
)
返迴
true
若
lhs
document is equal to
rhs
document,
false
否則。