您只需使用
Map
并使用
groovy.json.JsonOutput.toJson()
助手方法,例如。
def tool_name = 'test'
def product_name = 'test'
def platform_name = 'test'
def map = [name: tool_name , product: product_name , platform: platform_name]
def json = groovy.json.JsonOutput.toJson(map)
printlnâ jsonâ
此示例生成以下输出:
{'name': 'test', 'product': 'test', 'platform': 'test'}
如果要使用
groovy.json.JsonBuilder
然后,下面的示例生成您的预期输出:
def tool_name = 'test'
def product_name = 'test'
def platform_name = 'test'
def builder = new groovy.json.JsonBuilder()
builder {
name tool_name
product product_name
platform platform_name
}
println builder.toString()â
groovy.json.JsonSlurper
类专用于读取JSON文档并在需要时对其进行操作。