从下面的JSON中,我正在尝试编写一个Gpath,当“type”字段等于 ride_state 时,它只会返回“state”字段(即我只想让两个“打开” “国家”
[{
"type": "day",
"data": {
"id": "7a46a975-5474-4278-a56f-4deadd9a276d",
"state": "closed"
}
}, {
"type": "ride_state",
"data": {
"id": "13f3b625-b39e-4875-b29c-0173712b3c87",
"state": "open"
}
}, {
"type": "ride_state",
"data": {
"id": "b52b5081-e1df-42da-ba76-027c63205f8e",
"state": "open"
}
}]
我是Groovy和Gpath的新手,到目前为止还没有办法做到这一点,有人可以帮忙吗?
答案 0 :(得分:0)
使用有效的json,可以按照以下方式完成:
def jsonString = """
[{
"type": "day",
"data": {
"id": "7a46a975-5474-4278-a56f-4deadd9a276d",
"state": "closed"
}
}, {
"type": "ride_state",
"data": {
"id": "13f3b625-b39e-4875-b29c-0173712b3c87",
"state": "open"
}
}, {
"type": "ride_state",
"data": {
"id": "b52b5081-e1df-42da-ba76-027c63205f8e",
"state": "open"
}
}]
"""
assert new groovy.json.JsonSlurper()
.parseText( jsonString )
.findResults {it.type == 'ride_state' ? it.data.state : null} ==
['open', 'open']