新建一个Qt空项目
17Json.pro |
HEADERS MyWidget.h SOURCES MyWidget.cpp QT |
MyWidget.h |
#ifndef MYWIDGET_H #define MYWIDGET_H #include <QWidget> class MyWidget : public QWidget { Q_OBJECT public: explicit MyWidget(QWidget *parent); signals: public slots: }; #endif // MYWIDGET_H |
MyWidget.cpp |
#include "MyWidget.h" #include <QApplication> #include <QJsonDocument> #include <QJsonObject> #include <QDebug> #include <QCryptographicHash> //用于加密的头文件 /*json的数据类型*/ /* "{ "key": value [ {},{}, ] }" */ /* [ {}, {}, {} ] */ MyWidget::MyWidget(QWidget *parent) : QWidget(parent) { //使用md5加密 QByteArray md5 = QCryptographicHash::hash(QByteArray("涂作权"), QCryptographicHash::Md5); QJsonObject obj; obj.insert("username",QString("涂作权")); obj.insert("password",QString(md5.toHex())); obj.insert("male",true); obj.insert("age",24); QJsonDocument doc(obj); QByteArray json = doc.toJson(); qDebug() << json; //QJsonDocument doc2 = QJsonDocument::fromJson(json); //QJsonObject obj2 = doc2.object(); QJsonObject obj2 = QJsonDocument::fromJson(json).object(); if(obj2.value("username").toString() == "涂作权") { qDebug() << "yes"; } } int main(int argc,char** argv) { QApplication app(argc,argv); MyWidget w; w.show(); return app.exec(); } |
运行结果: |