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
How to save and load a QJsonDocument to a file?
提问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 QJsonDocument
and compare results.
我正在尝试学习如何使用JSON和Qt JSON 类。例如,我想创建一个简单的QJsonDocument
,将其保存到文件中,将其加载到不同的文件中QJsonDocument
并比较结果。
I managed to create a QJsonDocument
. However there is no simple command in the QJsonDocument
interfaceto 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
此答案显示了如何通过以下方式加载文档
- reading to a
QFile
- converting
QFile
to aQString
- converting the
QString
to aQByteArray
- constructing the
QJsonDocument
from theQByteArray
- 读到一个
QFile
- 转换
QFile
为QString
- 转换
QString
为QByteArray
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 QVariant
object from QJsonDocument
and save it with QSettings
. Look at functions QJsonDocument::fromVariantand QJsonDocument::toVariant. Combine them with QSettings
class and specifically void QSettings::setValue ( const QString & key, const QVariant & value )method, that works well with QVariant
and that's it.
无需转换为字符串并返回。使用QSettings和QVariant类,您可以轻松做到这一点。QVariant
从创建对象QJsonDocument
并使用QSettings
. 查看函数QJsonDocument::fromVariant和QJsonDocument::toVariant。将它们与QSettings
类结合起来,特别是void QSettings::setValue ( const QString & key, const QVariant & value )方法,这很好用QVariant
,就是这样。
Also QSettings
class has this constructor QSettings::QSettings ( const QString & fileName, Format format, QObject * parent = 0 )that would allow you to set path to the file - fileName
variable
也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 QFile
instead 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 加载/保存代码。