简体   繁体   中英

Qt 5.0 Json encoding

Im using qt 5.0 and its support such classes as QJsonObject QJsonDocument and QJsonArray. In my programm i need to serialize json array and convert it to qstring/qbytearray but i didn't found any serialize or encode methods in those classes. Is there any way i can serialize data using included qt 5.0. libs? I found this example:

QVariant id(1), name("John Doe");
QJsonObject json;

json["Name"] = name.toString();
json.insert("id", id.toInt());

But i can't find how i can make an array from it.

Question closed. Use QJsonDocument::toJson to get data from QJsonObject.

I think the secret is that when serializing to JSON, Qt writes out a UTF-8 binary encoded version of the text into a QByteArray . You can recover the string using QString::fromUtf8 . Also, even though toJson writes out a QByteArray it is not a true binary representation; as I just mentioned, it is the bytes of the UTF-8 encoded string representation of the JSON. For binary serialization you must use either Qt's own QJsonDocument::toBinaryData or find a BSON library.

Text serialization

Use QJsonDocument::toJson to serialize as text via a QString , and QJsonDocument::fromJson to deserialize from text.

Tip : Consider passing the QJsonDocument::Compact format option to get compact JSON output during serialization.

To QString

QString json = QString::fromUtf8(doc.toJson(QJsonDocument::Compact));

From QString

QJsonDocument doc = QJsonDocument::fromJson(json);

Binary serialization

Use QJsonDocument::toBinaryData to serialize as binary via a QByteArray , and QJsonDocument::fromBinaryData to deserialize from binary.

Caveat : The binary format used by the above functions is Qt's own binary format (qbjs) so may not be suitable for interoperation with other binary formats such as BSON . For that, you might want to look into choosing a BSON implementation .

To QByteArray

QByteArray bytes = doc.toBinaryData();

From QByteArray

QJsonDocument doc = QJsonDocument::fromBinaryData(bytes);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM