以下是我的 JSON 文件
{
"squadName": "Super hero squad",
"homeTown": "Metro City",
"formed": 2016,
"secretBase": "Super tower",
"active": true,
"members": [
{
"name": "Molecule Man",
"age": 29,
"secretIdentity": "Dan Jukes",
"powers": [
"Radiation resistance",
"Turning tiny",
"Radiation blast"
]
},
{
"name": "Madame Uppercut",
"age": 39,
"secretIdentity": "Jane Wilson",
"powers": [
"Million tonne punch",
"Damage resistance",
"Superhuman reflexes"
]
},
{
"name": "Eternal Flame",
"age": 1000000,
"secretIdentity": "Unknown",
"powers": [
"Immortality",
"Heat Immunity",
"Inferno",
"Teleportation",
"Interdimensional travel"
]
}
]
}
检索所有成员名称的JSON路径是 - $ .members [*]。name
我正在使用Ready API和Groovy脚本使用上面提到的路径读取这个json,但是当我执行下面的代码时遇到java.lang.NoClassDefFoundError: Could not initialize class net.minidev.json.JSONValue
的错误消息
import com.jayway.jsonpath.*
Object dataObject = JsonPath.parse(jsonmentionedabove).read(
'$.members[*].name')
我的Ready API lib&中包含以下jars
ext文件夹,并在我的电脑上有java版本 - 9.0.1
json-path-2.4.0, json-smart-2.3
您能告诉我导致此问题的原因吗?
答案 0 :(得分:1)
我不熟悉Ready API或者json-path和json-smart库,但是你可以做到纯粹的groovy:
import groovy.json.*
def str = """<the json string in your question>"""
def json = new JsonSlurper().parseText(str)
def memberNames = json.members*.name
println memberNames.join(", ")
执行时将打印:
Molecule Man, Madame Uppercut, Eternal Flame
JsonSlurper返回map结构的java.util.Map,可以使用普通常规findAll
,collect
等操作或spead operator(*.
)来导航在上面。
答案 1 :(得分:0)
在Ready API lib文件夹中放置asm-1.0.2.jar文件解决了这个问题。