QJsonValue findKey(const QString& key, const QJsonValue& value)
{
if (value.isObject()) {
QJsonObject obj = value.toObject();
if (obj.contains(key)) {
return obj.value(key); // return 'early' if object contains key
}
for (const auto& value : obj) {
QJsonValue recurse = findKey(key, value); // call itself, forwarding a value
if (!recurse.isNull())
return recurse; // value found, return 'early'
}
} else if (value.isArray()) {
for (const auto& value : value.toArray()) {
QJsonValue recurse = findKey(key, value);
if (!recurse.isNull())
return recurse;
}
}
return QJsonValue(); // base case: a null value
}
int main(int argc, char *argv[])
{
QFile file(":/res/scratch.json"); // json stored in a qrc with /res/ prefix
file.open(QIODevice::ReadOnly);
if (!file.isOpen()) {
qDebug() << "error: couldn't open scratch.json";
return 0;
}
QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
qDebug() << "value:" << findKey("treasure", doc.object());
}
scratch.json:
{
"deck": [
"first mate",
"sailor",
"john muir"
],
"cabin": [
{
"name": "lamp"
},
{
"name": "treasure chest",
"items": {
"diamonds": 3,
"silver": 5,
"gold": 10,
"safebox": {
"treasure": "shiny"
}
}
}
]
}
result:
value: QJsonValue(string, "shiny")