【问题标题】:How to convert java.lang.String to json in groovy如何在groovy中将java.lang.String转换为json
【发布时间】:2023-03-18 17:54:01
【问题描述】:

我的 java.lang.String 的格式是

 [[{"ABC":{"total":0,"failed":0,"skipped":0}}], [{"BCD": {"total":0,"failed":0,"skipped":0}}]]

如何在 groovy 中将其转换为 json?

【问题讨论】:

标签: groovy


【解决方案1】:

使用内置 groovy 工具从字符串中解析 json 是使用 groovy.json.JsonSlurper 完成的。你可以在here查看文档。

下面是访问示例 json 的方式,就像 groovy 嵌套地图一样:

def str = '[[{"ABC":{"total":0,"failed":0,"skipped":0}}], [{"BCD": {"total":0,"failed":0,"skipped":0}}]]'
def parser = new JsonSlurper()
def json = parser.parseText(str)
assert json[0][0].ABC.total == 0
assert json[0][0].ABC.failed == 0
assert json[0][0].ABC.skipped == 0
assert json[1][0].BCD.total == 0
assert json[1][0].BCD.failed == 0
assert json[1][0].BCD.skipped == 0

【讨论】:

    猜你喜欢
    • 2013-09-20
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多