在 Qt 的 JSON 文件中查找值 [英] Find a value in JSON file in Qt

查看:30
本文介绍了在 Qt 的 JSON 文件中查找值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 JSON 文件,例如:

I have a JSON file such like:

{
  "entries": [
    {
      "key": "8abd1c8c95f9f9c0f1c6d71e6cdece24",
      "val": "name1"
    },
    {
      "key": "f16d8c8d4163345bae5f5044d9ae7772",
      "val": "name2"
    },
    {
      "key": "9fa4b9915c6bae745f9400e08b391a6f",
      "val": "name3"
    },
    ...
  ]
}

我想搜索名称(例如 name3)并想知道密钥(例如 9fa4b9915c6bae745f9400e08b391a6f).如何使用递归函数在 Qt 中快速完成此操作?

and I want to search for the name (e.g. name3) and want to know the key (e.g. 9fa4b9915c6bae745f9400e08b391a6f). How can I do this very fast in Qt with a recursive function?

谢谢

推荐答案

您好,欢迎来到 Stack Overflow.一个简单的方法是使用 QJsonDocument(自 Qt 5 起可用)和基本线性搜索(我不知道您为什么要使用递归函数,这似乎效率很低,至少对于您提供的示例数据结构而言).

Hi and welcome to Stack Overflow. An easy way to do it is using the QJsonDocument (available since Qt 5) and a basic linear search (I'm not sure why you are asking for a recursive function, which seems very less efficient, at least for the sample data structure you've provided).

以下代码段假定文档的有效性(即没有错误检查).

The following snippet assumes the validity of the document (i.e., no error checking).

// 'text' is a variable containing the JSON data, probably read from a file
const QString text = R"({
    "entries": [
      {
        "key": "8abd1c8c95f9f9c0f1c6d71e6cdece24",
        "val": "name1"
      },
      {
        "key": "f16d8c8d4163345bae5f5044d9ae7772",
        "val": "name2"
      },
      {
        "key": "9fa4b9915c6bae745f9400e08b391a6f",
        "val": "name3"
      }
    ]
  })";

// Parse the document
const auto json = QJsonDocument::fromJson(text.toUtf8());

const auto entries = json["entries"];
for (const auto entry: entries.toArray()) {
  const auto obj = entry.toObject();
  if (obj["val"] == "name3") {
    qDebug() << "Key is" << obj["key"];
    break;
  }
}

请注意,JSON 的每个条目都被视为通用 QJsonValue,您必须将其转换为其实际类型(QJsonArrayQJsonObject...).

Note that each entry of the JSON is treated as a generic QJsonValue which you have to convert to its actual type (QJsonArray, QJsonObject...).

如果您需要查找多个值,您可以考虑将 JSON 预处理为更高效的数据结构,例如地图,然后在那里执行您的查询,而不是在文档上线性迭代.请注意,在这种情况下,我们假设构建逆映射的值是唯一的,因此键是 JSON 的值,值是键.

If you need to look up several values, you can consider preprocessing the JSON into a more efficient data structure like a map, and then perform your queries there, instead of iterating linearly on the document. Note that in this case we are assuming the values are unique to construct the inverse map, so the key is the value of the JSON, and the value is the key.

QMap<QString, QString> inv_map;
const auto entries = json["entries"];
for (const auto item : entries.toArray()) {
  const auto obj = item.toObject();
  inv_map[obj["val"].toString()] = obj["key"].toString();
}

QStringList names = {"name2", "name3", "name1"};
for (const auto &name : names) {
  const auto it = inv_map.find(name);
  if (it != inv_map.end()) {
    qDebug() << "Key for" << name << "is" << it.value();
  }
}

其他选项包括将列表存储在排序数组中(按值排序),并使用二分搜索来搜索具有所需值的条目.此解决方案的优点是,如果您有重复的值,您仍然可以获得所有这些值的键.

Further options include storing the list in a sorted array (sorted by value) and use a binary search to search for the entry with the value you want. This solution has the advantage that, if you have repeated values, you can still get the keys for all of them.

这篇关于在 Qt 的 JSON 文件中查找值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆