我有以下我想检查的 JSON 对象
import groovy.json.JsonSlurper
def jsonSlurper = new JsonSlurper()
import groovy.json.JsonOutput;
def object = jsonSlurper.parseText '''
{
"id" : 10,
"docType" : "PDF",
"values" : {
"color" : "red",
"pages" : 2,
},
"versions" : [
{
"verNum" : 1,
"desc" : "This is the description for it"
}
]
}
'''
// def data = new JsonSlurper().parseText("""[{"a": 1, "b": 2, "c": 3, "x": true}, {"a": 4, "b": 5, "c": 6, "d": "Hello"}]""")
// def content = object.collectEntries{
// it.collectEntries{
// [it.key, it.value.class.name]
// }
//}
//println content
我想遍历每个键并使用 Groovy 检查类型,例如:id - java.lang.Integer、docType - java.lang.String、values.color - java.lang.String、对象内的verNum数组内将是 java.lang.Integer
我已经搜索了几种不同的方法,但其中大多数都不适用于我的情况。其中一个现在像上面的代码一样被注释掉。
任何建议将不胜感激!