JSON在REST服务中被截断。可能是因为双引号?
我试图返回一个JSON响应,其中的文本可能包含双引号。比如说JSON在REST服务中被截断。可能是因为双引号?,json,rest,grails,escaping,Json,Rest,Grails,Escaping,我试图返回一个JSON响应,其中的文本可能包含双引号。比如说 "12.10 On-Going Submission of ""Made Up"" Samples." 我已使用 StringEscapeUtils.escapeJava(artifact.text); 完全没有引用 \"12.10 On-Going Submission of \"\"Made Up\"\" Samples.\" 但是JSON响应被截断了 { "documentName":"ICENSE AGREEMENT
"12.10 On-Going Submission of ""Made Up"" Samples."
我已使用
StringEscapeUtils.escapeJava(artifact.text);
完全没有引用
\"12.10 On-Going Submission of \"\"Made Up\"\" Samples.\"
但是JSON响应被截断了
{
"documentName":"ICENSE AGREEMENT6",
"listOfArtifacts":[
{
"type":"Clause",
"artifacts":[
{
"documentId":6951,
"documentName":"ICENSE AGREEMENT6",
"artifactId":7029,
"text":"\\\"12.10 On-Going Submission o\\\"\\\""
这是一个REST服务,我在其中手动返回了一个JSON。(这不是全部代码,但我希望您能理解。最终结果将呈现为JSON)
那么,即使转义是正确的,为什么JSON会被截断呢?在控制台上,我还收到一个错误:
2014-09-25 14:55:07,555 [http-bio-8080-exec-1] ERROR errors.GrailsExceptionResolver - StringIndexOutOfBoundsException occurred when processing request: [GET] /artifact - parameters:
documentName: ICENSE AGREEMENT6
String index out of range: -25. Stacktrace follows:
Message: String index out of range: -25
Line | Method
->> 1911 | substring in java.lang.String
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 1946 | subSequence in ''
| 1042 | append . . . . in java.io.PrintWriter
| 56 | append in ''
| 180 | value . . . . in grails.converters.JSON
| 162 | convertAnother in ''
| 202 | value . . . . in ''
| 162 | convertAnother in ''
| 202 | value . . . . in ''
| 162 | convertAnother in ''
| 202 | value . . . . in ''
| 134 | render in ''
| 150 | render . . . . in ''
| 328 | $tt__index in com.thomsonreuters.ald.aeandsdx.ArtifactController
| 198 | doFilter . . . in grails.plugin.cache.web.filter.PageFragmentCachingFilter
| 63 | doFilter in grails.plugin.cache.web.filter.AbstractFilter
| 1145 | runWorker . . in java.util.concurrent.ThreadPoolExecutor
| 615 | run in java.util.concurrent.ThreadPoolExecutor$Worker
^ 745 | run . . . . . in java.lang.Thread
您可以让JSON转换器为您完成这项工作:
String content = '"12.10 On-Going Submission of ""Made Up"" Samples."'
render ([text: content] as JSON)
@你能说得更具体些吗?您没有收到有效的JSON作为响应吗?这就是我的问题所在。我会得到一个JSON,但它在某个时刻被截断text@krs我的示例没有截断消息->
{“text”:“\”12.10正在提交由\“\”组成的\“\”样本。\”}
我正在将lsit列表呈现为JSON。这不是全部代码。我正在向rtVAL添加更多内容。这与您的示例有什么关系?您的json在text属性中被截断。我的示例显示您不需要StringEscapeUtils.escapeJava
和artifact.text.replace(“\”,“\ \”)
。你试过不用它吗?
String content = '"12.10 On-Going Submission of ""Made Up"" Samples."'
render ([text: content] as JSON)