GSON - 没有逃脱unicode char

Jef*_*rey 3 java json gson

我正在使用GSON来序列化我的对象.但是有些原因,它并没有逃脱某些unicode字符.

    Gson gson =  new Gson();
    System.out.println(gson.toJson("™"));
    System.out.println(Character.codePointAt("™", 0) );
    System.out.println(Character.codePointAt("™", 1) );
Run Code Online (Sandbox Code Playgroud)

输出:

"A™"

194

8482

是否有设置以确保字符被转义?

Den*_*ret 6

没有理由转义unicode字符,因为它们在JSON字符串中被允许.

来自json.org:

在此输入图像描述

如果您在将JSON发送到浏览器时遇到问题,请检查您的HTTP标头是否设置为UTF-8.

如果你真的想要像Java一样转义unicode字符,你可以使用apache commons的StringEscapeUtils#escapeJava,但你可能不应该这样做.