簡體   English   中英

Groovy JsonBuilder 在構建 Json 時添加奇怪的字符序列

[英]Groovy JsonBuilder adds strange character sequences when building Json

我在 Java 類中有一個授權枚舉:public enum FinalResponse {

THING_1("Thing1") {
    @Override
    String getDescription() {
        return "Based on what you've told us so far, it’s likely ; (it goes on)
    }

    @Override
    String getApplyUrl() {
        return "https://www.theinternet.com";
    }
},

當我從以下類調用 getDescription 時:

class FinalResponseMaker {

    FinalResponseMaker() {}

    static String getResponse(FinalResponse response) {
        def output = JsonOutput.toJson([[
            code: response.getCode(),                         
            description: response.getDescription(),                             
            apply_url: response.getApplyUrl()]])
    JsonOutput.prettyPrint(output)
    }
}

此代碼的輸出包含字符串

it\u2019s likely, 

即由於某種原因,“it's”中的引號已經變成了它的 unicode 字符。

為什么? 我怎樣才能讓這種情況停止發生?

我試過使用

    def pretty = JsonOutput.prettyPrint(output)
    def unescaped = JsonOutput.unescaped(pretty)
    return unescaped

但這不起作用。 任何幫助都會很棒,謝謝

不是世界上最漂亮的解決方案,但是......

description: new String(response.getDescription().getBytes("UTF8"))

這應該確保你不會得到任何逃脫的惡意。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM