I am trying to learn how to use JSON and the Qt JSON classes. For example I wnat to create a simple QJsonDocument
, save it to a file, load it into a different QJsonDocument
and compare results.
I managed to create a QJsonDocument
. However there is no simple command in the QJsonDocument
interface to save it to a file. The same goes for loading the document from a file.
#include <QJsonObject>
#include <QJsonDocument>
#include <QVariant>
int main()
{
QVariantMap map;
map.insert("integer", 1);
map.insert("double", 2.34);
map.insert("bool", QVariant(true));
map.insert("string", "word");
QJsonObject object = QJsonObject::fromVariantMap(map);
QJsonDocument document;
document.setObject(object);
// ?? save document to file
// ?? load file to document
return 0;
}
This answer shows how to load the document by
- reading to a
QFile
- converting
QFile
to aQString
- converting the
QString
to aQByteArray
- constructing the
QJsonDocument
from theQByteArray
Is there a more straightforward way to do this?