133
技術社區[雲棲]
關於http接口開發中json格式數據編碼問題處理
關於http接口開發中json格式數據編碼問題處理
在實際工作中,接口很多時候返回json格式,但有時返回的格式會有編碼問題
假設如下接口:https://service.test.com/interface/getData_test.jsp
返回如下是正常的。
[{"name":"\u8fd9\u662f\u4e2a\u6d4b\u8bd5"}]
但有時返回是這樣的。
[{"name":"這是個測試"}]
這個是錯誤的。
解決辦法:
主要是jar包引起的。
正確jar包如下:
json-2.2.1.jar
json-lib-2.4-jdk15.jar
如果:如下jar包 則有編碼問題
json-parser_fat.jar
json-lib-2.4-jdk15.jar
測試代碼如下:
<%@page import="org.apache.commons.lang.math.NumberUtils"%><%@page import="java.net.URLEncoder"%><% response.setContentType("text/html;charset=UTF-8"); response.setHeader("Cache-Control", "no-cache"); response.setHeader("Cache-Control", "no-store"); response.setDateHeader("Expires", 0); response.setHeader("Pragma", "no-cache"); %><%@page import="java.sql.Timestamp"%><%@page contentType="text/html; charset=UTF-8" %><%@ page import="java.util.*, java.io.IOException,java.util.regex.*, org.springframework.web.context.WebApplicationContext, org.springframework.web.context.support.WebApplicationContextUtils, org.apache.commons.lang.StringUtils,net.sf.json.*" %><%=getJX_Content()%><%! /** * 測試 * @return */ public static String getJX_Content() { String ret=""; JSONArray jsonArray=new JSONArray(); JSONObject jsonObject = new JSONObject(); jsonObject.put("name","這是個測試"); jsonArray.add(jsonObject); ret=jsonArray.toString(); return ret; } %>
相關json包下載: https://download.csdn.net/detail/5iasp/6198283
最後更新:2017-04-03 16:49:04