我正在尝试在Grails中解析随机JSON文件。
首先,我需要获取每个字段的名称
例如,在下面的JSON文件中,
{
"abbreviation": "EX",
"guid": "1209812-1l2kj1j-fwefoj9283jf-ae",
"metadata": {
"dataOrigin": "Example"
},
"rooms":
[
],
"site": {
"guid": "1209812-1l2kj1j-fwefoj9283jf-ae"
},
"title": "Example!!"
}
我想找出JSON文件的结构(可能是键列表),例如,我想保存键列表,例如“缩写”,“guid”,“元数据”,“rooms”,“site”,此JSON文件中的“标题”。
我该怎么做?
(我们需要键的名称才能获取该键的值,因此,对于任意结构的JSON文件,我需要先找出键)
最佳答案
您可以尝试以下代码
def filePath = "JSONFILE.json"
def text = new File(filePath).getText()
def json = JSON.parse(text)
def jsonKeys = json.collect{it.key}
println(jsonKeys)
这将打印所有json键
关于json - 获取JSON中每个字段的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30597048/