项目中用到了一些JSON的东西,为了方便就做了一层简单的封装方便以后使用,包括了JSON的读取、解析、生成和保存这些基本功能
json.h文件
#ifndef JSON_H
#define JSON_H
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>
class JSON {
public:
JSON();
QJsonObject getJson();
QJsonObject loadJson(const QString& filepath);
void writeJson(const QString key, bool value);
void writeJson(const QString key, int value);
void writeJson(const QString key, double value);
void writeJson(const QString key, QString value);
void writeJson(const QString key, bool* array, int length);
void writeJson(const QString key, int* array, int length);
void writeJson(const QString key, double* array, int length);
void writeJson(const QString key, QJsonObject object);
bool saveJson(const QString& filepath);
QString toString();
private:
QJsonObject json;
};
#endif // JSON_H
json.cpp文件
#include <QDebug>
#include <QFile>
#include <QIODevice>
#include "json.h"
JSON::JSON()
{
}
QJsonObject JSON::getJson()
{
return json;
}
QJsonObject JSON::loadJson(const QString& filepath)
{
QFile loadFile(filepath);
if