Gson Unicode字符无法正确显示以响应前端

时间:2015-01-07 17:46:18

标签: java json spring-mvc gson unicode-string

我有一个返回json对象的服务,其中summary字段包含未正确显示的unicode字符。此字符串中的unicode字符无法在响应中正确显示。回复示例:

summary: "A boyfriend and girlfriend?s deaths in Arlington, being investigated as a murder-suicide, shock relatives."

“女朋友”这个词的单引号出现了?字符。我该如何解决这个问题?相关代码是:

FileReader fileReader = new FileReader(jsonFilePath);
BufferedReader buffered = new BufferedReader(fileReader);
gson= new GsonBuilder().disableHtmlEscaping().create();
RecommendationResponseDto recommendationResponseDto;
recommendationResponseDto = gson.fromJson(buffered,RecommendationResponseDto.class);

来自外部服务的响应示例,它返回由上述gson目标代码处理的unicode摘要,如下所示:

u'A boyfriend and girlfriend\u2019s deaths in Arlington, being investigated as a murder-suicide, shock relatives.'

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

永远不要使用FileReader。相反,明确指定字符集:

Reader reader = new InputStreamReader(new FileInputStream(file), "UTF-8");

另请参阅有关该主题的blog post I wrote