MyJsonParser(const QJsonValue& jsonValue, const QString& fieldName = "") {
parseJsonValue(jsonValue, myStruct, fieldName);
MyJsonParser getNext(const QString& key) const {
newParser.myStruct = findStruct(key, myStruct);
QString getValueOrFieldName() const {
if (!fieldName.isEmpty()) {
} else if (!myStruct.subMap.isEmpty()) {
return myStruct.subMap.keys().first();
} else if (!myStruct.arrayValues.isEmpty()) {
void parseJsonValue(const QJsonValue& jsonValue, MyStruct& currentStruct, const QString& fieldName) {
if (jsonValue.isObject()) {
const QJsonObject jsonObj = jsonValue.toObject();
parseJsonObject(jsonObj, currentStruct, fieldName);
} else if (jsonValue.isArray()) {
const QJsonArray jsonArray = jsonValue.toArray();
parseJsonArray(jsonArray, currentStruct, fieldName);
currentStruct.value = jsonValue.toString();
this->fieldName = fieldName;
void parseJsonObject(const QJsonObject& jsonObj, MyStruct& currentStruct, const QString& fieldName) {
for (auto it = jsonObj.begin(); it != jsonObj.end(); ++it) {
QJsonValue value = it.value();
parseJsonValue(value, currentStruct.subMap[key], key);
void parseJsonArray(const QJsonArray& jsonArray, MyStruct& currentStruct, const QString& fieldName) {
for (const QJsonValue& value : jsonArray) {
parseJsonValue(value, arrayElement, fieldName);
currentStruct.arrayValues.append(arrayElement);
MyStruct findStruct(const QString& key, const MyStruct& currentStruct) const {
QStringList keys = key.split('.');
const MyStruct* structPtr = ¤tStruct;
for (const QString& k : keys) {
if (k.toInt() >= 0 && k.toInt() < structPtr->arrayValues.size()) {
structPtr = &structPtr->arrayValues[k.toInt()];
auto it = structPtr->subMap.find(k);
if (it == structPtr->subMap.end()) {
int main(int argc, char** argv) {
QString jsonString = R"({
QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonString.toUtf8());
QJsonObject jsonObj = jsonDoc.object();
MyJsonParser parser(jsonObj);
QString nextInfo = parser.getNext("a.e").getValueOrFieldName();
QString nextInfo1 = parser.getNext("a.f").getValueOrFieldName();
QString nextInfoArr = parser.getNext("a.b.c").getValueOrFieldName();
QString nextInfo2 = parser.getNext("a.b.c").getNext("0.d").getValueOrFieldName();
QString nextInfo3 = parser.getNext("a.b.c").getNext("1.d").getValueOrFieldName();
QString nextInfoErr = parser.getNext("a.g").getValueOrFieldName();
QString nextInfoErr1 = parser.getNext("a.h").getValueOrFieldName();
qDebug() << "Next Info at a.e:" << nextInfo;
qDebug() << "Next Info at a.f:" << nextInfo1;
qDebug() << "Next Info at a.b.c:" << nextInfoArr;
qDebug() << "Next Info at a.b.c.0.d:" << nextInfo2;
qDebug() << "Next Info at a.b.c.1.d:" << nextInfo3;
qDebug() << "Next Info at a.g:" << nextInfoErr;
qDebug() << "Next Info at a.h:" << nextInfoErr1;