下面是我的常规代码,我使用 json 数据构建命令,但是 json 数据具有不同类型的数据,例如数组中的列表,或者只有单个变量,所以任何人都可以告诉我如何查找变量的数据类型。
下面的代码我突出显示了一个元素“jsondata[key]”,它是我的键值,我想检查这个值的数据类型,就像在我的 JSON 数据中一样,我有 params(key) 和 4 个数组列表(值)所以在使用参数值之前我已经检查了数据类型。
key : [1 2 3]
if (typeof(key) == list) {
for (value in key) {
#here i want to frame a command using previous keys..
}
}
Run Code Online (Sandbox Code Playgroud)
就像Python中的typeof()
import groovy.json.JsonSlurper
def label = "test testname params"
File jsonFile = newFile("/home/developer/Desktop/kramdeni/vars/PARAMS.json")
def jsondata = new JsonSlurper().parse(jsonFile)
println jsondata.keySet()
println "jsondata: " + jsondata
def command = ""
keys = label.split(" ")
println "keys: " + keys
for (key in keys) {
command += "-" + key + " " + **jsondata[key]** + " "
}
println "command: " + command
Run Code Online (Sandbox Code Playgroud)
{
"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"
}
]
}
Run Code Online (Sandbox Code Playgroud)
小智 6
jsondata.each{ entry->
println entry.value.getClass()
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10821 次 |
| 最近记录: |