C++ 如何将 QJsonDocument 保存和加载到文件中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21260075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:34:27  来源:igfitidea点击:

How to save and load a QJsonDocument to a file?

c++jsonqtqt5

提问by Martin Drozdik

I am trying to learn how to use JSONand the Qt JSON classes. For example I wnat to create a simple QJsonDocument, save it to a file, load it into a different QJsonDocumentand compare results.

我正在尝试学习如何使用JSONQt JSON 类。例如,我想创建一个简单的QJsonDocument,将其保存到文件中,将其加载到不同的文件中QJsonDocument并比较结果。

I managed to create a QJsonDocument. However there is no simple command in the QJsonDocumentinterfaceto save it to a file. The same goes for loadingthe document from a file.

我设法创建了一个QJsonDocument. 但是,QJsonDocument界面中没有简单的命令可以将其保存到文件中。这同样适用于加载该文件中的文件。

#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 answershows how to load the document by

此答案显示了如何通过以下方式加载文档

  1. reading to a QFile
  2. converting QFileto a QString
  3. converting the QStringto a QByteArray
  4. constructing the QJsonDocumentfrom the QByteArray
  1. 读到一个 QFile
  2. 转换QFileQString
  3. 转换QStringQByteArray
  4. QJsonDocument从构建QByteArray

Is there a more straightforward way to do this?

有没有更直接的方法来做到这一点?

采纳答案by Shf

No need for converting to string and back. With QSettingsand QVariantclasses you can easily do that. Create QVariantobject from QJsonDocumentand save it with QSettings. Look at functions QJsonDocument::fromVariantand QJsonDocument::toVariant. Combine them with QSettingsclass and specifically void QSettings::setValue ( const QString & key, const QVariant & value )method, that works well with QVariantand that's it.

无需转换为字符串并返回。使用QSettingsQVariant类,您可以轻松做到这一点。QVariant从创建对象QJsonDocument并使用QSettings. 查看函数QJsonDocument::fromVariantQJsonDocument::toVariant。将它们与QSettings类结合起来,特别是void QSettings::setValue ( const QString & key, const QVariant & value )方法,这很好用QVariant,就是这样。

Also QSettingsclass has this constructor QSettings::QSettings ( const QString & fileName, Format format, QObject * parent = 0 )that would allow you to set path to the file - fileNamevariable

QSettings类有此构造QSettings :: QSettings(常量QString的和文件名,格式格式,QObject的*父= 0) ,将允许您设置文件的路径-fileName变量

回答by John Chadwick

Personally, I think that code [that you linked to] looks a bit messy. Warning: head compiled code follows.

就个人而言,我认为 [您链接到的] 代码看起来有点乱。警告:头部编译代码如下。

QJsonDocument loadJson(QString fileName) {
    QFile jsonFile(fileName);
    jsonFile.open(QFile::ReadOnly);
    return QJsonDocument().fromJson(jsonFile.readAll());
}

void saveJson(QJsonDocument document, QString fileName) {
    QFile jsonFile(fileName);
    jsonFile.open(QFile::WriteOnly);
    jsonFile.write(document.toJson());
}

This may not be perfect: it assumes QFileinstead of QIODevice, but if you're dealing with only local files maybe it won't matter. You can then use these functions instead of repeating the Json load/save code everytime you need to load/save Json.

这可能并不完美:它假设QFile而不是QIODevice,但如果您只处理本地文件,则可能无关紧要。然后,您可以使用这些函数,而无需在每次需要加载/保存 Json 时重复执行 Json 加载/保存代码。