首页 > 解决方案 > 在其他本地化 json 文件中查找相同的键

问题描述

我想在其他 jsons 本地化文件中找到相同的键。我的json看起来像这样。

{
  "general": {
    "main": {
      "title": "This is a title",
      "header": "This is a header",
      "sub": {
        "sub_thing_1": "Something",
        "sub_thing_2": "Something2"
      }
    }
  },
  "other_things": {
    "title": "Other title"
    "sub": {
       "sub_thing_1": "Something",
       "sub_thing_2": "Something2"
    }
  }
}

如您所见,相同的键可以在更多地方

我的想法是,要生成一个包含这样的深度值的集合

例如:这个“sub_thing_2”可以在 -> ["general", "main", "sub"] 找到

有了这些信息,我可以在其他文件中找到确切的密钥。

通过这段代码,我收集了每个键的深度信息,这只是一个划痕,思考代码片段,顺便说一下,我是 Kotlin 的新手


psi?.accept(object : JsonRecursiveElementVisitor() {
    override fun visitObject(o: JsonObject) {
        val childrens = o.children
        for (child in childrens) {
            val childProp = child as JsonProperty
            if (child.lastChild is JsonObject) {
                depth.add(childProp.name)
                visitObject(child.lastChild as JsonObject)
            } else {
                println(depth.toString() + childProp.name + ": " + childProp.lastChild.text)
            }
        }

        if (depth.isNotEmpty()) {
            val lastItem = depth.last();
            depth.remove(lastItem)
        }
    }
})

我的问题是有其他更简单的方法可以做到这一点,也许没有 PSI 或者我的方向是正确的?

标签: intellij-ideakotlinintellij-plugin

解决方案


推荐阅读