【问题标题】:Groovy JsonBuilder adds strange character sequences when building JsonGroovy JsonBuilder 在构建 Json 时添加了奇怪的字符序列
【发布时间】:2018-07-26 08:26:02
【问题描述】:

我在 Java 类中有一个授权枚举: 公共枚举 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

但这不起作用。任何帮助都会很棒,谢谢

【问题讨论】:

    标签: java json groovy escaping


    【解决方案1】:

    不是世界上最漂亮的解决方案,但是……

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

    这应该确保您不会得到任何逃脱的恶意。

    【讨论】:

      猜你喜欢
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-14
      • 1970-01-01
      • 2015-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多