JSON在REST服务中被截断。可能是因为双引号?

时间:2014-09-25 20:34:29

标签: 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 AGREEMENT6",
"listOfArtifacts":[
{
"type":"Clause",
"artifacts":[
{
"documentId":6951,
"documentName":"ICENSE AGREEMENT6",
"artifactId":7029,
"text":"\\\"12.10 On-Going Submission o\\\"\\\"" 

这是一个REST服务,我手动返回了一个JSON。 (这不是整个代码,但我希望你能得到这个想法。最后结果呈现为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会被截断?同样在控制台上我收到一个错误:

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

1 个答案:

答案 0 :(得分:1)

您可以让JSON转换器为您完成工作:

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