【问题标题】:How can I save JSON to local text file in QT?如何将 JSON 保存到 QT 中的本地文本文件?
【发布时间】:2021-10-13 04:23:28
【问题描述】:
jsonlist.push(Object.assign(process1_json,process2_json,process3_json,process4_json,process5_json, process6_json))

var j = JSON.stringify(jsonlist);

我可以使用console.log 来获取 JSON 输出,但我想生成一个本地 JSON 文件。

好像 JavaScript 不能直接读/写本地文件,所以如果我必须将 JSON 转换为 QJson 然后获取一个本地 JSON 文件?

【问题讨论】:

  • 一种方法是将j 连接到C++ 后端,然后使用QFileQtextStream

标签: javascript qt qml qfile


【解决方案1】:

您可以通过在 C++ 中将您的 JSON 转换为 QJsonObject,然后使用该函数保存它:

bool writeJson(const QString &fileName, const QJsonObject &object)
{
   //open file
   QFile file(fileName);
   if(!file.open(QIODevice::WriteOnly | QIODevice::Text)){
       qDebug() << QString("Fail to open the file:"+fileName);
       return false;
   }

   //convert to document
   QJsonDocument d = QJsonDocument(object);
   //write and close
   file.write(d.toJson());
   file.close();
   return true;
}

【讨论】:

    【解决方案2】:

    还是谢谢你。我通过引入 C++ 模块解决了这个问题。

    #ifndef FILECONTENT_H
    #define FILECONTENT_H
    
    #include <QObject>
    #include <QFile>
    #include <QTextStream>
    
    class FileContent : public QObject
    {
        Q_OBJECT
    public:
        Q_PROPERTY(QString content READ getContent WRITE setContent)
        Q_PROPERTY(QString filename READ getFileName WRITE setFileName)
        Q_PROPERTY(QString dir READ getDir WRITE setDir)
        Q_INVOKABLE QString getContent();
        Q_INVOKABLE void setContent(QString s);
        Q_INVOKABLE QString getFileName();
        Q_INVOKABLE QString getDir();
        Q_INVOKABLE void setDir(QString s);
        FileContent(QObject *parent = 0);
        ~FileContent();
    private:
        QFile   *file;
        QString content;
        QString filename;
        QString dir;
    public slots:
        void setFileName(const QString& filename);
        void clearContent();
    };
    
    #endif // FILECONTENT_H
    
    #include "filecontent.h"
    #include <QDebug>
    
    FileContent::FileContent(QObject *parent) {
    
    }
    
    FileContent::~FileContent() {
        delete file;
    }
    
    QString FileContent::getFileName() {
        return this->filename;
    }
    
    QString FileContent::getDir()
    {
    //I need to get file from specific directory path 
        QString envVarName_equip = "MPCVD_MODEL";
        this->dir=qEnvironmentVariable(envVarName_equip.toStdString().c_str());
        return dir+ '\\';
    }
    
    void FileContent::setDir(QString s)
    {
        file = new QFile(s);
    }
    
    void FileContent::setFileName(const QString &filename) {
        this->filename = filename;
    
        file = new QFile(getDir() + '\\' + filename);
    
    }
    
    QString FileContent::getContent() {
    
        if( content.length() == 0 ) {
            file->open(QIODevice::ReadOnly | QIODevice::Text);
            QTextStream in(file);
            in.setCodec("UTF-8");//set format here
            content = in.readAll();
            if( content.length() == 0) {
                qDebug() << "[Warning] FileContent: file " << this->filename << "is empty" << endl;
            }
        }
        return content;
    }
    
    void FileContent::setContent(QString s)
    {
    
        file->open(QIODevice::WriteOnly | QIODevice::Text);
        QTextStream in(file);
        in.setCodec("UTF-8");//set format here
        in<<s;
    
    }
    
    void FileContent::clearContent() {
        content.clear();
    }
    

    main.cpp注册后,

    qmlRegisterType<FileContent>("test.filecontent", 1, 0, "FileContentItem");
    

    只是导入这个模块,

    import test.filecontent 1.0
    

    并使用如下:

    Button{
                id:genjson
                text: qsTr("generate JSON")
                onClicked: {
                    
                    fileDialog.processContent(new Date().toLocaleString(Qt.locale(), 'yyyyMMdd')+".mpcvd",CJS.genjson())
                }
            }
    
    
    FileContentItem {
                id: fileDialog
                filename: "default.mpcvd"
                property bool ready: false
    
                function processContent(source,content) {
    
    
                    if( source !== undefined ) {
                        filename = source;
                    }
    
                    setContent(content);
    
                    clearContent();  // save memory
                }
            }
    
    

    【讨论】:

    • 我不确定,但您可能会在 setDir() 和 setFileName() c++ 函数中泄漏内存。除了在解构器上之外,您调用“新”而没有相应的删除。需要注意的地方
    猜你喜欢
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    相关资源
    最近更新 更多