git-svn-id: svn://192.168.0.12/source@21 8346c931-da38-4b9b-9d4c-e48b93cbd075

This commit is contained in:
admin
2015-02-12 05:09:22 +00:00
parent c6db89c4e4
commit 64a018a082
4 changed files with 100 additions and 0 deletions

22
Json/Json.pro Normal file
View File

@@ -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

16
Json/main.cpp Normal file
View File

@@ -0,0 +1,16 @@
#include <QCoreApplication>
#include "sjson.h"
#include <QDebug>
#include <QFile>
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();
}

45
Json/sjson.cpp Normal file
View File

@@ -0,0 +1,45 @@
#include "sjson.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QDebug>
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();
}

17
Json/sjson.h Normal file
View File

@@ -0,0 +1,17 @@
#ifndef SJSON_H
#define SJSON_H
#include <QString>
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