QJsonDocument Class

The QJsonDocument class provides a way to read and write JSON documents. More...

Header: #include <QJsonDocument>
qmake: QT += core
Since: Qt 5.0

This class was introduced in Qt 5.0.

Note: All functions in this class are reentrant.

Public Types

enum DataValidation { Validate, BypassValidation }
enum JsonFormat { Indented, Compact }

Detailed Description

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() and isObject(). The array or object contained in the document can be retrieved using array() or object() and then read or manipulated.

A document can also be created from a stored binary representation using fromBinaryData() or fromRawData().

See also JSON Support in Qt and JSON Save Game Example.

Member Type Documentation

enum QJsonDocument::DataValidation

This value is used to tell QJsonDocument whether to validate the binary data when converting to a QJsonDocument using fromBinaryData() or fromRawData().

ConstantValueDescription
QJsonDocument::Validate0Validate the data before using it. This is the default.
QJsonDocument::BypassValidation1Bypasses 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 using toJson().

ConstantValueDescription
QJsonDocument::Indented0Defines human readable output as follows:
     {
         "Array": [
             true,
             999,
             "string"
         ],
         "Key": "Value",
         "null": null
     }
QJsonDocument::Compact1Defines a compact output as follows:
     {"Array":[true,999,"string"],"Key":"Value","null":null}

This enum was introduced or modified in Qt 5.1.