QJsonDocument 类

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 )

详细描述

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

This value is used to tell QJsonDocument whether to validate the binary data when converting to a 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 Defines a compact output as follows:
{"Array":[true,999,"string"],"Key":"Value","null":null}
								

成员函数文档编制

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

删除文档。

Binary data set with fromRawData is not freed.

QJsonArray QJsonDocument:: array () const

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

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

QByteArray QJsonDocument:: toBinaryData () const

Returns a binary representation of the document.

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 ( JsonFormat format = Indented) const

转换 QJsonDocument 成 UTF-8 编码 JSON 文档在提供 format .

另请参阅 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 。返回此对象的引用。

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

返回 true other 文档等于此文档。