REST在REST服务中被截断。可能是因为双引号? [英] JSON get truncated in REST service. Possible because of double quotes?

查看:209
本文介绍了REST在REST服务中被截断。可能是因为双引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用可能在其中包含双引号的文本返回JSON响应。例如

I am trying to return a JSON response with text that may contain double quotes in them. For example

 "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)

This is a REST service where I have manually returned a JSON. (This is not the entire code but I hope you get the idea. in the end the result is rendered as JSON)

    def index() {

        def document
        def artifacts
        def documentId
        def documentName
        def artifactType
        def artifactStatus
        def includeClassifications
        def classifications
        def mapOfAtifactTypes = [:]
        def listOfArtifacts = []
        def listOfClassifications = []
        def rtnVal = [:]

        documentId = params.documentId
        documentName = params.documentName

        try {    
                if (artifacts) {  
                        def k = StringEscapeUtils.escapeJava(artifact.text); 
                        def artifactToAdd = [
                                documentId: artifact.documentId,
                                documentName: artifact.document.docName,
                                artifactId: artifact.id,

                                //text: k,    
                                text: artifact.text.replace("\"","\\\""),           
                                status: artifact.status ?: Artifact.ArtifactStatus.FOR_REVIEW.value,
                                hasClassification: listOfClassifications ? true : false
                        ];

                        listOfArtifacts.add(artifactToAdd)
                        }
                    rtnVal.listOfArtifacts = []
                    mapOfAtifactTypes.each { entry ->
                        rtnVal.listOfArtifacts.add([
                                type: entry.key,
                                artifacts: entry.value
                                ])
                    }
            }

        } catch (Exception e) {
            e.printStackTrace()
            rtnVal = [
                    status: "Bad request",
                    msg: e
            ]
            render e
        }        
        render rtnVal as JSON
    }

即使转义正确,为什么JSON被截断?另外在控制台上我收到一个错误:

So even with the escaping being correct why is the JSON getting truncated ? Also on the console I am getting an error :

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转换器为你做的工作:

You can let the JSON converter do the job for you:

String content = '"12.10 On-Going Submission of ""Made Up"" Samples."'
render ([text: content] as JSON)

这篇关于REST在REST服务中被截断。可能是因为双引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆