[英]Groovy httpbuilder ResponseParseException Invalid JSON string
[英]Groovy HttpBuilder json input trouble
我在将json参数传递到网络操作时遇到问题。 我知道该Web操作可以在指定的URL http://projects.example.net/example/bugnetwebservice.asmx/MobileBuildAction
,因为我已经使用邮递员使用json参数对其进行了测试:
{
featureIdStr: 31,
actionStr: 1,
comment: "Hello world"
}
并得到响应:
{
"d": "Succeeded"
}
每当我尝试以常规方式运行它时,都会收到以下响应:
Jun 10, 2016 9:54:25 AM net.sf.json.JSONObject _fromBean
INFO: Property 'value' of class org.codehaus.groovy.runtime.GStringImpl has no read method. SKIPPED
Failure: 500
这是我的代码:
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1')
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*
def http = new HTTPBuilder("http://projects.example.net/")
def issueId = 31
def msg = "Build Failed"
def jsonBody = [:]
jsonBody.put("featureIdStr", issueId)
jsonBody.put("actionStr", 0)
jsonBody.put("comment", "${msg}: <a href='http://www.google.com'}'>Googles Job</a>")
http.request(POST, JSON) {
uri.path = "/example/bugnetwebservice.asmx/MobileBuildAction"
body = jsonBody
response.success = { resp ->
println "Success! ${resp.status}"
}
response.failure = { resp ->
println "Failure: ${resp.status}"
}
}
请帮忙!
jsonBody.put(“ comment”,“ $ {msg}:http://www.google.com'}'> Googles Job”)
Groovy中的“”会创建一个Groovy字符串(又名GString)。 GString非常棒-它们允许使用${}
语法-但它们在自身进行序列化和反序列化方面存在一些问题。 有一个很棒的StackOverflow答案解释了这是怎么回事 。
无论如何,它的不足之处在于那篇文章和我自己的经验:每当您比较或可能要序列化Groovy String时,请首先在其上调用toString()
。
我会考虑编写您的代码,例如:
def commentValue = "${msg}: <a href='http://www.google.com'}'>Googles Job</a>"
jsonBody.put( commentValue.toString() )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.