I want to find the same key in an other jsons localization file.
(我想在其他jsons本地化文件中找到相同的键。)
My json's looks like this. (我的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"
}
}
}
As you can see the same key can be in more places
(如您所见,相同的键可以在更多地方)
My thinking is, to generate a collection that contains, the depth values like this
(我的想法是,要生成一个包含这样的深度值的集合)
eg: this "sub_thing_2" can be found at -> ["general", "main", "sub"]
(例如:此“ sub_thing_2”可以在-> [“ general”,“ main”,“ sub”]中找到)
and with this information i can find the exact key in other files.
(有了这些信息,我可以在其他文件中找到确切的密钥。)
And with this code I geathering the depth information for each key, it's just a scratch, thinkering code fragement, I'm new to Kotlin by the way
(借助此代码,我可以收集每个键的深度信息,这只是从头开始,思考代码的琐碎,顺便说一下,我对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)
}
}
})
My question is there is other easier way to do this maybe without the PSI or I'm in the right direction?
(我的问题是,如果没有PSI,或者我朝正确的方向,还有其他更简便的方法吗?)
ask by Kurucz Dávid translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…