#include "jsondecoderloop.h" #include #include #include #include #include JsonDecoderLoop::JsonDecoderLoop() { } QString JsonDecoderLoop::decode(const QByteArray byteArray, QString key) { // Check input if ( byteArray.isEmpty() || 0 == key.compare("")) { return ""; } QString str = ""; QJsonParseError jsonError; QJsonDocument jsonDoc = QJsonDocument::fromJson(byteArray, &jsonError); if(jsonError.error == QJsonParseError::NoError) { if(jsonDoc.isObject()) { QJsonObject obj = jsonDoc.object(); // Loop get value according the key world str = getValueBykey(obj, key); } } return str; } QString JsonDecoderLoop::getValueBykey(QJsonObject obj, QString key) { QString rst = ""; QStringList strList = obj.keys(); for (int i = 0; i < obj.size(); i++) { QJsonValue val = obj.value(strList.at(i)); if ( strList.at(i) == key && val.isString()) { rst = val.toString(); } else if (val.isObject()) { rst = getValueBykey(val.toObject(), key); } } return rst; }