65 lines
1.3 KiB
C++
65 lines
1.3 KiB
C++
#include "operatorjson.h"
|
|
#include <QJsonDocument>
|
|
#include <QFile>
|
|
#include <QJsonArray>
|
|
#include <QJsonObject>
|
|
#include <QByteArray>
|
|
OperatorJson::OperatorJson(QObject * parent ):QObject (parent)
|
|
{
|
|
|
|
}
|
|
OperatorJson::~OperatorJson()
|
|
{
|
|
|
|
}
|
|
|
|
bool OperatorJson::savaJson(QJsonObject obj, QString sPathFile)
|
|
{
|
|
{
|
|
|
|
QJsonDocument doc(obj);
|
|
QByteArray data = doc.toJson();
|
|
QFile file(sPathFile);//json文件
|
|
bool ok = file.open(QIODevice::WriteOnly);
|
|
if (ok)
|
|
{
|
|
file.write(data);
|
|
file.close();
|
|
}
|
|
return ok;
|
|
|
|
}
|
|
}
|
|
|
|
bool OperatorJson::readJson(QString sPathFile, QJsonObject & json)
|
|
{
|
|
//读入所有内容
|
|
QFile file(sPathFile);
|
|
|
|
file.open(QIODevice::ReadOnly);
|
|
QByteArray data = file.readAll();
|
|
file.close();
|
|
//解析
|
|
QJsonParseError jsonError;
|
|
QJsonArray jsonAarray;
|
|
|
|
QByteArray array(data.toStdString().c_str());
|
|
QJsonDocument document = QJsonDocument::fromJson(array, &jsonError);
|
|
|
|
if (jsonError.error == QJsonParseError::NoError)
|
|
{
|
|
//logInfo() << array << endl;
|
|
//jsonAarray = document.array();
|
|
json = document.object();
|
|
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
}
|