代码之家  ›  专栏  ›  技术社区  ›  Jonik

您能避免Gson将“<”和“>”转换成unicode转义序列吗?

  •  107
  • Jonik  · 技术社区  · 15 年前

    我注意到 Gson 在JSON输出中将字符串“<”转换为unicode转义序列。您能不能避免这种情况,或者像“<”和“>”这样的字符总是必须用JSON转义?

    考虑这个打印 {"s":"\u003c"} ;我只想 {"s":"<"} .

    public static void main(String[] args) {
        Gson gson = new GsonBuilder().create();
        System.out.println(gson.toJson(new Foo()));  
    }
    
    static class Foo {
        String s = "<";
    }
    

    1 回复  |  直到 15 年前
        1
  •  235
  •   Bernardo Ferrari stefandouganhyde    6 年前

    你需要 disable HTML escaping

    Gson gson = new GsonBuilder().disableHtmlEscaping().create();