如何使用httpbuilder在groovy中将http get响应检索为完整的json字符串
我想使用GET请求的响应json作为另一个请求的输入。因此,我收到的响应应该是正确的json格式。我正在使用HttpBuilder来实现这一点如何使用httpbuilder在groovy中将http get响应检索为完整的json字符串,json,groovy,http-get,httpbuilder,Json,Groovy,Http Get,Httpbuilder,我想使用GET请求的响应json作为另一个请求的输入。因此,我收到的响应应该是正确的json格式。我正在使用HttpBuilder来实现这一点 HTTPBuilder http = new HTTPBuilder(urlParam, ContentType.JSON); http.headers.Accept = ContentType.JSON; http.parser[ContentType.JSON] = http.parser.'application/json'
HTTPBuilder http = new HTTPBuilder(urlParam, ContentType.JSON);
http.headers.Accept = ContentType.JSON;
http.parser[ContentType.JSON] = http.parser.'application/json'
return http.request(GET) {
response.success = {resp, json ->
return json.toString()
}
当我返回json.toString()时,它不是格式良好的json。我如何做到这一点。当我单击get url时,我看到了整个json,但没有使用上面的代码。感谢您的帮助。With
groovy.json.JsonOutput
:
HTTPBuilder http = new HTTPBuilder('http://date.jsontest.com/', ContentType.JSON);
http.headers.Accept = ContentType.JSON
http.parser[ContentType.JSON] = http.parser.'application/json'
http.request(Method.GET) {
response.success = { resp, json ->
println json.toString() // Not valid JSON
println JsonOutput.toJson(json) // Valid JSON
println JsonOutput.prettyPrint(JsonOutput.toJson(json))
}
}
结果:
{time=09:41:21 PM, milliseconds_since_epoch=1497303681991, date=06-12-2017}
{"time":"09:41:21 PM","milliseconds_since_epoch":1497303681991,"date":"06-12-2017"}
{
"time": "09:41:21 PM",
"milliseconds_since_epoch": 1497303681991,
"date": "06-12-2017"
}