在Groovy中获取JSON数据

n3schb8v  于 2022-11-01  发布在  其他
关注(0)|答案(1)|浏览(149)

我需要从JSON中获取一些数据,我可以设法将其转换为String。例如,如果team role id为4,我需要获取amount值。(JSON中的最后一个作用域。)当我运行下面的代码时,“result”输出为
{id=1,有效日期=2003-01-01,货币代码=USD,汇率=[{id=1,汇率表={id=1,有效日期=2003-01-01,货币代码=USD,名称=克里思默认价格表,默认表=false},金额=0.0,链接={type=DEFAULT_RATE}}],名称=速度默认价格表,默认表=true}
我怎样才能得到完整的数据?谢谢。

http.request(Method.GET) {
    response.success = { resp, json ->

        arrayDen = JsonOutput.toJson(json).substring(1, JsonOutput.toJson(json).length()-1)
    }
}
        def slurper = new groovy.json.JsonSlurper()
        def result = slurper.parseText(arrayDen)
        log.warn(result)
[
    {
        "id": 1,
        "rateTable": {
            "id": 1,
            "effectiveDate": "2003-01-01",
            "currencyCode": "USD",
            "name": "Tempo Default Price Table",
            "defaultTable": false
        },
        "amount": 0.0,
        "link": {
            "type": "DEFAULT_RATE"
        }
    },
    {
        "id": 2,
        "rateTable": {
            "id": 3,
            "effectiveDate": "2022-03-21",
            "currencyCode": "USD",
            "name": "Rate",
            "defaultTable": false
        },
        "amount": 0.0,
        "link": {
            "type": "DEFAULT_RATE"
        }
    },
    {
        "id": 3,
        "rateTable": {
            "id": 3,
            "effectiveDate": "2022-03-21",
            "currencyCode": "USD",
            "name": "Rate",
            "defaultTable": false
        },
        "amount": 200.0,
        "link": {
            "type": "TEAM_ROLE",
            "id": 8
        }
    },
    {
        "id": 4,
        "rateTable": {
            "id": 3,
            "effectiveDate": "2022-03-21",
            "currencyCode": "USD",
            "name": "Rate",
            "defaultTable": false
        },
        "amount": 500.0,
        "link": {
            "type": "TEAM_ROLE",
            "id": 5
        }
    },
    {
        "id": 5,
        "rateTable": {
            "id": 3,
            "effectiveDate": "2022-03-21",
            "currencyCode": "USD",
            "name": "Rate",
            "defaultTable": false
        },
        "amount": 1000.0,
        "link": {
            "type": "TEAM_ROLE",
            "id": 4
        }
    }
]

`

ruyhziif

ruyhziif1#

嗯。以下内容对我来说很有效:

def arrayDen = '<YOUR_URL_GOES_HERE>'.toURL().text

def slurper = new groovy.json.JsonSlurper()
def result = slurper.parseText(arrayDen)

def desiredData = result.find { it.id == 4 }
println desiredData.amount

你可以给予看。

相关问题