【问题标题】:JSON get truncated in REST service. Possible because of double quotes?JSON 在 REST 服务中被截断。可能是因为双引号?
【发布时间】:2014-09-25 20:34:28
【问题描述】:

我正在尝试返回带有 可能 包含双引号的文本的 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

【问题讨论】:

    标签: json rest grails escaping


    【解决方案1】:

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

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

    【讨论】:

    • @krs 你能说得更具体点吗?您没有收到有效的 JSON 作为响应吗?
    • 我的问题是什么。我会得到一个 JSON,但它在文本中的某个时间点被截断
    • @krs 我的示例没有截断消息 -> {"text":"\"12.10 On-Going Submission of \"\"Made Up\"\" Samples.\""}
    • 我将 lsit 列表呈现为 JSON。这不是完整的代码。我正在向 rtVAL 添加更多内容。这与您的示例有何关系?
    • 您的 json 在 text 属性中被截断。我的示例向您展示了您不需要StringEscapeUtils.escapeJava 和artifact.text.replace("\"","\\\"")。你试过没有它吗?
    猜你喜欢
    • 1970-01-01
    • 2012-11-05
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 2010-09-20
    • 1970-01-01
    • 2023-01-13
    • 1970-01-01
    相关资源
    最近更新 更多