使用groovy

时间:2017-04-21 18:26:34

标签: json parsing groovy response

4用于使用httpbuilder检查进程的状态并获得以下响应。

  {"result":[{"id":"167687","dapadmin":"false","status":"in progress","lastupdated":"2017-04-21 14:34:30.0","started":"2017-04-21 14:34:28.0","user":"sys","log":"Running a Stop action\n\nRunning command \n"}]}

无法使用jsonSlurper.parseText()解析此响应错误

当我使用`

def json = JsonOutput.toJson(statusresponse)    
println JsonOutput.prettyPrint(json)    

我可以看到它被打印为json对象

{
    "result": [
        {
            "id": "167687",
            "dapadmin": "false",
            "status": "in progress",
            "lastupdated": "2017-04-21 14:34:30.0",
            "started": "2017-04-21 14:34:28.0",
            "user": "dapsystem",
            "log": "Running a Stop action\n\nRunning command \n"
        }
    ]
}    

当我检查getClass()时,它打印为java.lang.String。

我需要从此响应中获取lastupdated和status,id值。请帮我找到解决方案。

非常感谢

嗨炎培。

感谢您的回复。

我正在使用您建议的以下代码。

    def statusresponse = http.putText(baseurl,path,query,headerMap, Method.GET)
             println("The status response value is"  )
             statusresponse.each{ k, v -> 
                 println "${k}:${v}" 
             }

             def json = JsonOutput.toJson(statusresponse)
              println "JSON Object List : " + json
              println "------------------"
                              println JsonOutput.prettyPrint(json)
                println "The result class  is "+json.getClass()
                println "The result status  is "+(json instanceof Map)


                //def entry = slurper.parseText(json)

                def slurper = new groovy.json.JsonSlurper()
                def entry = slurper.parseText(statusresponse)
                def lastupdated = entry.result.lastupdated
                checkStatus = entry.result.status
                def id = entry.result.id        

得到以下错误

       The result class  is class java.lang.String
        Caught: groovy.lang.MissingMethodException: No signature of method: groovy.json.JsonSlurper.parseText() is applicable for argument types: (java.util.HashMap) values: [[result:[[id:170089, dapadmin:false, status:in progress, ...]]]]
        Possible solutions: parseText(java.lang.String), parse(java.io.Reader)
groovy.lang.MissingMethodException: No signature of method: groovy.json.JsonSlurper.parseText() is applicable for argument types: (java.util.HashMap) values: [[result:[[id:170089, dapadmin:false, status:in progress, ...]]]]
        Possible solutions: parseText(java.lang.String), parse(java.io.Reader)
    at dap.Main.main(Main.groovy:171)

如果我使用下面的代码

,它会起作用
 def json = JsonOutput.toJson(statusresponse)
 def entry = slurper.parseText(json)

我的结果如下

The status of the action is :[in progress]
Last updated [2017-04-23 17:08:02.0]
the id is[170088]       

首先,我不确定为什么建议的代码会抛出此错误 其次,为什么我在括号内得到工作解决方案的结果?

1 个答案:

答案 0 :(得分:0)

def slurper = new groovy.json.JsonSlurper()
def entry = slurper.parseText('{"result":[{"id":"167687","dapadmin":"false","status":"in progress","lastupdated":"2017-04-21 14:34:30.0","started":"2017-04-21 14:34:28.0","user":"sys","log":"Running a Stop action\n\nRunning command \n"}]}')
def lastupdated = entry.result.lastupdated
def status = entry.result.status
def id = entry.result.id

应该有效。无法看到您的错误,因此我无法提供更好的信息。