diff --git a/Json/Json.pro b/Json/Json.pro new file mode 100644 index 0000000..af7600e --- /dev/null +++ b/Json/Json.pro @@ -0,0 +1,22 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2015-02-11T18:41:42 +# +#------------------------------------------------- + +QT += core + +QT -= gui + +TARGET = Json +CONFIG += console +CONFIG -= app_bundle + +TEMPLATE = app + + +SOURCES += main.cpp \ + sjson.cpp + +HEADERS += \ + sjson.h diff --git a/Json/main.cpp b/Json/main.cpp new file mode 100644 index 0000000..4268697 --- /dev/null +++ b/Json/main.cpp @@ -0,0 +1,16 @@ +#include +#include "sjson.h" +#include +#include +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + QFile file; + file.setFileName("test.txt"); + file.open(QIODevice::ReadOnly | QIODevice::Text); + SJson test; + QString str = test.Set(file.readAll(),"tea","babo"); + qDebug() << test.Set(str,"sig",false); + file.close(); + return a.exec(); +} diff --git a/Json/sjson.cpp b/Json/sjson.cpp new file mode 100644 index 0000000..e5fbd1e --- /dev/null +++ b/Json/sjson.cpp @@ -0,0 +1,45 @@ +#include "sjson.h" +#include +#include +#include +#include + +SJson::SJson() +{ + +} + +SJson::~SJson() +{ + +} + +QString SJson::Get(QString _str,QString _strKey) +{ + return QJsonDocument::fromJson(_str.toUtf8()).object().value(_strKey).toString(); +} + +bool SJson::GetBool(QString _str,QString _strKey) +{ + return QJsonDocument::fromJson(_str.toUtf8()).object().value(_strKey).toBool(); +} + +QString SJson::Set(QString _str,QString _strKey,QString _strValue) +{ + QJsonDocument doc = QJsonDocument::fromJson(_str.toUtf8()); + QJsonObject obj = doc.object(); + obj.insert(_strKey,QJsonValue(_strValue)); + doc.setObject(obj); + return doc.toJson(); +} + +QString SJson::Set(QString _str,QString _strKey,bool _bValue) +{ + QJsonDocument doc = QJsonDocument::fromJson(_str.toUtf8()); + QJsonObject obj = doc.object(); + obj.insert(_strKey,QJsonValue(_bValue)); + doc.setObject(obj); + return doc.toJson(); +} + + diff --git a/Json/sjson.h b/Json/sjson.h new file mode 100644 index 0000000..1013e04 --- /dev/null +++ b/Json/sjson.h @@ -0,0 +1,17 @@ +#ifndef SJSON_H +#define SJSON_H + +#include + +class SJson +{ +public: + SJson(); + ~SJson(); + QString Get(QString _str,QString _strKey); + bool GetBool(QString _str,QString _strKey); + QString Set(QString _str,QString _strKey,QString _strValue); + QString Set(QString _str,QString _strKey,bool _bValue); +}; + +#endif // SJSON_H