Json 如何在groovy中查找变量的数据类型
下面是我的groovy代码,我正在使用json数据构建一个命令,但是json数据有不同类型的数据,比如数组中的list,或者只有一个变量,所以任何人都可以告诉我如何查找变量的数据类型 在下面的代码中,我突出显示了一个元素“jsondata[key]”,这是我的键值,我想检查这个值的数据类型,就像在我的JSON数据中一样,我有4个数组(值)列表的params(key),所以在使用params值之前,我要检查数据类型 我的预期结果: 类似于python中的typeof() 我的json数据:Json 如何在groovy中查找变量的数据类型,json,groovy,Json,Groovy,下面是我的groovy代码,我正在使用json数据构建一个命令,但是json数据有不同类型的数据,比如数组中的list,或者只有一个变量,所以任何人都可以告诉我如何查找变量的数据类型 在下面的代码中,我突出显示了一个元素“jsondata[key]”,这是我的键值,我想检查这个值的数据类型,就像在我的JSON数据中一样,我有4个数组(值)列表的params(key),所以在使用params值之前,我要检查数据类型 我的预期结果: 类似于python中的typeof() 我的json数据: 以下代
以下代码说明了
String
和List
类型的instanceof
:
import groovy.json.JsonSlurper
def label = "test testname params"
def jsonFile = new File("PARAMS.json")
def jsondata = new JsonSlurper().parse(jsonFile)
def command = ""
def keys = label.split(" ")
for (key in keys) {
def value = jsondata[key]
if (value instanceof String) {
println "${key} ${value}"
} else if (value instanceof List) {
value.each { item ->
println "${key} contains ${item}"
}
} else {
println "WARN: unknown data type"
}
}
指定JSON的示例输出(我不确定如何构建命令
,因此这是一个简单的输出。根据需要构建命令
应该很容易):
以下代码说明了
String
和List
类型的instanceof
:
import groovy.json.JsonSlurper
def label = "test testname params"
def jsonFile = new File("PARAMS.json")
def jsondata = new JsonSlurper().parse(jsonFile)
def command = ""
def keys = label.split(" ")
for (key in keys) {
def value = jsondata[key]
if (value instanceof String) {
println "${key} ${value}"
} else if (value instanceof List) {
value.each { item ->
println "${key} contains ${item}"
}
} else {
println "WARN: unknown data type"
}
}
指定JSON的示例输出(我不确定如何构建命令
,因此这是一个简单的输出。根据需要构建命令
应该很容易):
key.getClass()
有什么问题?在groovy/java中,可以使用key.getClass()或(key instanceof String)。但是您案例中的所有键都将具有字符串数据类型。key.getClass()
有什么问题吗?在groovy/java中,您可以使用key.getClass()或(key instanceof String)。但是您案例中的所有键都将具有字符串数据类型。
{
"test": "iTEST",
"testname": "BOV-VDSL-link-Rateprofile-CLI-Test-1",
"params": [
{
"n2x_variables/config_file": "C:/Program Files (x86)/Agilent/N2X/RouterTester900/UserData/config/7.30 EA SP1 Release/OSP Regression/BOV/Bov-data-1-single-rate-profile.xml"
},
{
"n2x_variables/port_list": "303/4 303/1"
},
{
"n2x_variables/port_list": "302/3 303/4"
},
{
"n2x_variables/port_list": "301/3 303/5"
}
]
}
jsondata.each{ entry->
println entry.value.getClass()
}
import groovy.json.JsonSlurper
def label = "test testname params"
def jsonFile = new File("PARAMS.json")
def jsondata = new JsonSlurper().parse(jsonFile)
def command = ""
def keys = label.split(" ")
for (key in keys) {
def value = jsondata[key]
if (value instanceof String) {
println "${key} ${value}"
} else if (value instanceof List) {
value.each { item ->
println "${key} contains ${item}"
}
} else {
println "WARN: unknown data type"
}
}
$ groovy Example.groovy
test iTEST
testname BOV-VDSL-link-Rateprofile-CLI-Test-1
params contains [n2x_variables/config_file:C:/Program Files (x86)/Agilent/N2X/RouterTester900/UserData/config/7.30 EA SP1 Release/OSP Regression/BOV/Bov-data-1-single-rate-profile.xml]
params contains [n2x_variables/port_list:303/4 303/1]
params contains [n2x_variables/port_list:302/3 303/4]
params contains [n2x_variables/port_list:301/3 303/5]