8编码内容转换为问号

cs7cruho  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(342)

我有一个json数组(jsonarray类型),包含utf-8编码字符串:

[{
      "success":true,
      "data":[
         {"moduleTitle":"تست",
          "title":"تست دو"}
       ],
       "status":200
    }]

然后我想得到它的字符串值。

@RequestMapping(value = "/document-types", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
    public @ResponseBody
    ResponseEntity<String> getJobDocumentTypes(@RequestBody String body) throws BusinessException, org.activiti.engine.impl.util.json.JSONException, org.json.JSONException {
.
.
.
String s = String.valueOf(jsonArray);
return new ResponseEntity<String>(s, HttpStatus.OK);
}

但utf-8字符串将变成问号,如下所示:
[{“success”:true,“data”:[{“moduletitle”:“???”,“title”:“?????”}],“状态“:200}]
如何使它正确显示字符串?

xj3cbfub

xj3cbfub1#

您正在使用jvm的默认字符集转换数据,utf-8字符集很可能不在您的服务器或本地服务器上。
试着用这个

String s = String.valueOf(jsonArray);
String string2 = new String(s.getBytes(),"UTF-8");
return new ResponseEntity<String>(string2, HttpStatus.OK);

相关问题