Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

QJsonDocument Class

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

 #include <QJsonDocument>

Note: All functions in this class are reentrant.

This class was introduced in Qt 5.0.

Public Types

enum DataValidation { Validate, BypassValidation }

Public Functions

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() const
QVariant toVariant() const
bool operator!=(const QJsonDocument & other) const
QJsonDocument & operator=(const QJsonDocument & other)
bool operator==(const QJsonDocument & other) const

Static Public Members

QJsonDocument fromBinaryData(const QByteArray & data, DataValidation validation = Validate)
QJsonDocument fromJson(const QByteArray & json, QJsonParseError * error = 0)
QJsonDocument fromRawData(const char * data, int size, DataValidation validation = Validate)
QJsonDocument fromVariant(const QVariant & variant)

Detailed Description

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

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.

Member Function Documentation

QJsonDocument::QJsonDocument()

Constructs an empty and invalid document.

QJsonDocument::QJsonDocument(const QJsonObject & object)

Creates a QJsonDocument from object.

QJsonDocument::QJsonDocument(const QJsonArray & array)

Constructs a QJsonDocument from array.

QJsonDocument::QJsonDocument(const QJsonDocument & other)

Creates a copy of the other document.

QJsonDocument::~QJsonDocument()

Deletes the document.

Binary data set with fromRawData is not freed.

QJsonArray QJsonDocument::array() const

Returns the QJsonArray contained in the document.

Returns an empty array if the document contains an object.

See also isArray, object, and setArray.

QJsonDocument QJsonDocument::fromBinaryData(const QByteArray & data, DataValidation validation = Validate) [static]

Creates a 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.

See also toBinaryData, fromRawData, isNull, and DataValidation.

QJsonDocument QJsonDocument::fromJson(const QByteArray & json, QJsonParseError * error = 0) [static]

Parses a UTF-8 encoded JSON document and creates a QJsonDocument from it. isNull() will return false if no error was encountered during parsing.

See also toJson.

QJsonDocument QJsonDocument::fromRawData(const char * data, int size, DataValidation validation = Validate) [static]

Creates a QJsonDocument that uses the first size bytes from 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 has to be aligned to a 4 byte boundary.

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.

Returns a QJsonDocument representing the data.

See also rawData, fromBinaryData, isNull, and DataValidation.

QJsonDocument QJsonDocument::fromVariant(const QVariant & variant) [static]

Creates a QJsonDocument from the QVariant variant.

If the variant contains any other type than a QVariant::Map(obsolete), QVariant::List(obsolete) or QVariant::StringList(obsolete), the returned document document is invalid.

See also toVariant.

bool QJsonDocument::isArray() const

Returns true if the document contains an array.

See also array() and isObject().

bool QJsonDocument::isEmpty() const

Returns true if the document doesn't contain any data.

bool QJsonDocument::isNull() const

returns true if this document is null.

Null documents are documents created through the default constructor.

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

Returns true if the document contains an object.

See also object() and isArray().

QJsonObject QJsonDocument::object() const

Returns the QJsonObject contained in the document.

Returns an empty object if the document contains an array.

See also isObject, array, and setObject.

const char * QJsonDocument::rawData(int * size) const

Returns the raw binary representation of the data size will contain the size of the data.

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)

Sets array as the main object of this document.

See also setObject and array.

void QJsonDocument::setObject(const QJsonObject & object)

Sets object as the main object of this document.

See also setArray and 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.

See also fromBinaryData.

QByteArray QJsonDocument::toJson() const

Converts the QJsonDocument to a UTF-8 encoded JSON document.

See also fromJson.

QVariant QJsonDocument::toVariant() const

Returns a QVariant representing the Json document.

The returned variant will be a QVariantList if the document is a QJsonArray and a QVariantMap if the document is a QJsonObject.

See also fromVariant and QJsonValue::toVariant().

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

returns true if other is not equal to this document

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

Assigns the other document to this QJsonDocument. Returns a reference to this object.

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

Returns true if the other document is equal to this document.

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 5.0-snapshot
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web