例如有如下某JSON文件app.json。app是一个数组,包含三个应用程序.exe,args是启动参数。
{
"app": [
{
"name": "Ink",
"path": "E:\\Ink.exe",
"args": ""
},
{
"name": "Mes",
"path": "E:\\Mes.exe",
"args": "m1"
},
{
"name": "Pri",
"path": "E:\\Pri.exe",
"args": "c1"
}
}
解析该json文件,读取内容,并显示在tableWidget内显示。
.h文件
#pragma once
#include <QMainWindow>
namespace Ui { class MainWindow; }
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget* pParent = nullptr);
virtual ~MainWindow();
private slots:
void SaveToFile();//保存时用到的槽函数
private:
Ui::MainWindow* m_pUI;
}
.cpp文件
解析JSON并填入表格
#include "MainWindow.h"
#include <fstream>
#include <QFile>
#include <QDir>
#include <QtDebug>
#include <QFileDialog>
#include <QListWidget>
#include <QTextStream>
#include <rapidjson/document.h>
#include <rapidjson/istreamwrapper.h>
#include <rapidjson/pointer.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>
#include "ui_MainWindow.h"
static const char* s_szConfFileName = "App.json";
MainWindow::MainWindow(QWidget* pParent)
: QMainWindow(pParent),
m_pUI(new Ui::MainWindow)
{
m_pUI->setupUi(this);
QString strConfigFilePath = QCoreApplication::applicationDirPath() +
QDir::separator() + s_szConfFileName;//读取程序所在文件夹+App.json
std::string str =QDir::toNativeSeparators(strConfigFilePath).toStdString();//Qstring转化为string,还有分隔符\转化为//
std::fstream fs(str, std::ios::in);
if (!fs)
{
return;
}
json::Document doc;
doc.ParseStream(json::IStreamWrapper(fs));
if (doc.HasParseError() || !doc.IsObject())
{
qDebug()<<("fail to open job file:{}", s_szConfFileName) ;
return;
}
const json::Value *pValApp = json::Pointer("/app").Get(doc);
if (pValApp && pValApp->IsArray())
{
int nRow = m_pUI->tableWidget->rowCount();
for (auto it = pValApp->Begin(); it != pValApp->End(); ++it)
{
QTableWidgetItem *pName = new QTableWidgetItem;
QTableWidgetItem *pPath = new QTableWidgetItem;
QTableWidgetItem *pArgs = new QTableWidgetItem;
const json::Value* pValAppName = json::Pointer("/name").Get(*it);
if (pValAppName && pValAppName->IsString())
{
pName->setText(pValAppName->GetString());
}
const json::Value* pValAppPath = json::Pointer("/path").Get(*it);
if (pValAppPath && pValAppPath->IsString())
{
pPath->setText(QString::fromLatin1(pValAppPath->GetString()));
}
const json::Value* pValAppArgs = json::Pointer("/args").Get(*it);
if (pValAppArgs && pValAppArgs->IsString())
{
pArgs->setText(pValAppArgs->GetString());
}
m_pUI->tableWidget->insertRow(nRow); //动态增加行数
m_pUI->tableWidget->setItem(nRow, 0, pName);//把pName放入第nRow行,第1列
m_pUI->tableWidget->setItem(nRow, 1, pPath);
m_pUI->tableWidget->setItem(nRow, 2, pArgs);
++nRow;
}
}
}
保存表格内容为JSON文件 同为.cpp内函数
/*是一个名叫SaveToFile的槽函数,可以自己在UI上拖一个Button,对象名为SaveButton发射信号,连接函数可以这样写在上面的cpp内(记得在头文件里定义此槽函数哦):
connect(m_pUI->SaveButton, &QPushButton::clicked, this, &MainWindow::SaveToFile);*/
void MainWindow::SaveToFile() //保存tableWidget信息
{
json::Document doc(json::kObjectType);
json::Document::AllocatorType& alloc = doc.GetAllocator();
json::Value valApp(json::kArrayType);
for (int i = 0; i < m_pUI->tableWidget->rowCount(); ++i)
{
QTableWidgetItem *ItemN = m_pUI->tableWidget->item(i, 0);
std::string strNa = ItemN->text().toStdString();//读取内容
QTableWidgetItem *ItemP = m_pUI->tableWidget->item(i, 1);
std::string strPa = ItemP->text().toStdString();
QTableWidgetItem *ItemA = m_pUI->tableWidget->item(i, 2);
std::string strAr = ItemA->text().toStdString();
json::Value valJob(json::kObjectType);
valJob.AddMember("name", strNa, alloc);
valJob.AddMember("path", strPa, alloc);
valJob.AddMember("args", strAr, alloc);
valApp.PushBack(valJob, alloc);
}
doc.AddMember("app", valApp, alloc);//添加数组的名字
json::StringBuffer sbuf;
json::Writer<json::StringBuffer> writer(sbuf);
doc.Accept(writer);
QString strConfigFilePath = QCoreApplication::applicationDirPath() + QDir::separator() + s_szConfFileName;
std::string str = QDir::toNativeSeparators(strConfigFilePath).toStdString();
std::fstream fs(str, std::ios::out);//保存文件
if (fs)
{
fs << sbuf.GetString();
}
}