简体   繁体   English

GSON - 没有逃脱unicode char

[英]GSON - Not escaping unicode char

I am using GSON to serialize my object. 我正在使用GSON来序列化我的对象。 But some reason, it is not escaping certain unicode chars. 但是有些原因,它并没有逃脱某些unicode字符。

    Gson gson =  new Gson();
    System.out.println(gson.toJson("™"));
    System.out.println(Character.codePointAt("™", 0) );
    System.out.println(Character.codePointAt("™", 1) );

Output: 输出:

"™" “A™”

194 194

8482 8482

Is there a setting to ensure that the chars get escaped? 是否有设置以确保字符被转义?

There is no reason to escape unicode characters as they're allowed in JSON strings. 没有理由转义unicode字符,因为它们在JSON字符串中被允许。

From json.org : 来自json.org

在此输入图像描述

If you have problems sending your JSON to the browser, check your HTTP headers are set to UTF-8. 如果您在将JSON发送到浏览器时遇到问题,请检查您的HTTP标头是否设置为UTF-8。

If you really want to escape unicode characters as in Java, you might use apache commons's StringEscapeUtils#escapeJava but you probably shouldn't. 如果你真的想要像Java一样转义unicode字符,你可以使用apache commons的StringEscapeUtils#escapeJava,但你可能不应该这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM