Groovy中从字符串到映射的groovycastexception

Groovy中从字符串到映射的groovycastexception,groovy,Groovy,我正在使用Groovy在ready api中实现一个项目。 我创建了一个具有以下结构的Groovy脚本文件 def templateValue = JsonOutput.toJson(''' { "generationTime": null, "house": { "property": "$property", "state": "$state", "county": "$county", "calls": [ { "fir

我正在使用Groovy在ready api中实现一个项目。 我创建了一个具有以下结构的Groovy脚本文件

def templateValue = JsonOutput.toJson('''
{
  "generationTime": null,
  "house": {

    "property": "$property",
    "state": "$state",
    "county": "$county",
    "calls": [
      {
        "first": "$first",
        "second": "$second",
        "third": "third Week"
      }
    ],
    "action": "$action"
  }
}''')
def binding = ["property" : "Villa", "state" : "MA", "county" : "MIDDLE", "first" : "123", "second" : "4565", "action" : "Update"]
def engine = new groovy.text.SimpleTemplateEngine()
def template = engine.createTemplate(templateValue).make(binding)
log.info template.toString()

def jsonSlurper = new JsonSlurper()
Map expectedOutput = jsonSlurper.parseText(template.toString())
log.info expectedOutput["property"]

我需要从json中获取节点值,然后从json中删除特定节点。

您不需要顶部的
JsonOutput.toJson

试试这个:

def templateValue = '''
{
  "generationTime": null,
  "house": {

    "property": "$property",
    "state": "$state",
    "county": "$county",
    "calls": [
      {
        "first": "$first",
        "second": "$second",
        "third": "third Week"
      }
    ],
    "action": "$action"
  }
}'''
def binding = ["property" : "Villa", "state" : "MA", "county" : "MIDDLE", "first" : "123", "second" : "4565", "action" : "Update"]
def engine = new groovy.text.SimpleTemplateEngine()
def template = engine.createTemplate(templateValue).make(binding)

def jsonSlurper = new groovy.json.JsonSlurper()
Map expectedOutput = jsonSlurper.parseText(template.toString())
println expectedOutput.house.property

谢谢。快速提问。如何从这个JSOn中删除节点。比如说,我想从json中取出“calls”数组。明白了。expectedOutput.house.remove(“调用”)可以很好地删除节点