json1串:
{"orderCount":0,"signType":"SHA256WithRSA","sign":"85022E420F6F0429A2A1F8774C66C04BFF6085CEE37C6DC085F366733081689C16639B03B34455B7BAA2659E82FF8FD8A031643DA7D9A24B2F2898404758C0BD64F889E1B0C58BE0ADBFF59C292BCA4B55DC2CDA052A8AB964D99F1F2497386AA482A458F49442BD119DEBB7C2C90ACD8E2906C16290A810FAE8357D2D6175F10111D8DDBD108443F6D515AABD9B4879AF0D18B08257698E0FA2899CE012EEF1A6DFEEC25C6FFCEABD019AA70CE5CE13C388811FB55392D576B9EA55D3F45A426064262CA953D67A8E15B79E9DF42066D48B60FB0950EB6415FFC546C1D11EE2DF8A28907F8C65FA3B4D42514103C3CDC72594295E7874B6B3C72487333DCF9B","returnCode":"1","returnDesc":"系统执行错误"}

json2串:

"{\"orderCount\":0,\"signType\":\"SHA256WithRSA\",

\"sign\":\"85022E420F6F0429A2A1F8774C66C04BFF6085CEE37C6DC085F366733081689C16639B03B34455B7BAA2659E82FF8FD8A031643DA7D9A24B2F2898404758C0BD64F889E1B0C58BE0ADBFF59C292BCA4B55DC2CDA052A8AB964D99F1F2497386AA482A458F49442BD119DEBB7C2C90ACD8E2906C16290A810FAE8357D2D6175F10111D8DDBD108443F6D515AABD9B4879AF0D18B08257698E0FA2899CE012EEF1A6DFEEC25C6FFCEABD019AA70CE5CE13C388811FB55392D576B9EA55D3F45A426064262CA953D67A8E15B79E9DF42066D48B60FB0950EB6415FFC546C1D11EE2DF8A28907F8C65FA3B4D42514103C3CDC72594295E7874B6B3C72487333DCF9B\",\"returnCode\":\"1\",\"returnDesc\":\"系统执行错误\"}"

报错内容:

Can not instantiate value of type [map type; class java.util.LinkedHashMap, [simple type, class java.lang.Object] -> [simple type, class java.lang.Object]] from JSON String; no single-String constructor/factory method


原因:
使用中涉及如下两个方法:
public void sendText(HttpServletResponse response, String str)
			throws IOException {


		// 必须写上这句,中文编码问题
		response.setContentType("text/plain;charset=UTF-8");
		//response.setContentType("application/json;charset=utf-8");


		response.getWriter().write(str);
		response.getWriter().flush();
		response.getWriter().close();
}


public void sendResponse(HttpServletResponse response, Object object)
			throws IOException {
		logger.debug("响应的对象:{}", object);
		Gson gson = new Gson();
		String respJson = gson.toJson(object);
		
		logger.debug("响应的数据:{}", respJson);
		logger.debug("响应的状态:{}", response);
		sendText(response, respJson);
}


首先我将上述json1字符串传入sendResponse方法,其内部会对json1字符串再做一下json转换,此时得到json2串,即带有转译字符。
调用者获取json2串使用ObjectMapper转换成Map便会报上述错误。


解决方法:
     直接调用sendText方法发出响应。
GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e 4 个月前
8c391e04 6 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐