最近做报表功能时,从后台取的json数据出现中文乱吗,如下图:
![](https://img.haomeiwen.com/i3132804/f2684eeaad2069e5.png)
然后进行了一些列的设置,比如
response.setContentType("text/html,charset=gbk");
response.setCharacterEncoding("gbk");
然而依旧如故,在网上了查找了大量资料,终于找了一个解决方案,利用流进行输出,制定编码,便可解决。
packagecom.dodonew.mediaplatform.action;
importjava.io.IOException;
importjava.io.PrintWriter;
importjava.nio.charset.Charset;
importjavax.servlet.ServletException;
importjavax.servlet.ServletOutputStream;
importjavax.servlet.annotation.WebServlet;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importcom.alibaba.fastjson.JSONArray;
importcom.alibaba.fastjson.JSONObject;
importcom.dodonew.mediaplatform.utils.Constants;
importjvc.module.JObject;
importjvc.util.DBUtils;
importjvc.util.DateUtils;
@WebServlet("/gamegraph")
publicclassGameGraphicalextendsHttpServlet {
privatestaticfinallongserialVersionUID= 1L;
publicGameGraphical() {
super();
}
protectedvoiddoGet(HttpServletRequestrequest, HttpServletResponseresponse)throwsServletException, IOException {
ServletOutputStreamout=response.getOutputStream();
//response.setContentType("text/html,charset=gbk");
//response.setCharacterEncoding("gbk");
StringlastDate=DateUtils.date();
JSONObjectjson=newJSONObject();
JSONArrayjst=newJSONArray();
JSONArrayjsts=newJSONArray();
String[]gameName=newString[]{"英雄联盟","地下城与勇士","穿越火线","多塔","QQ飞车","QQ炫舞","逆战","梦幻西游","天涯明月刀","大话西游2","大话西游3","剑灵","QQ","使命召唤"};
JSONArraygames;
for(inti=0;i
games=newJSONArray();
for(intj=14;j>0;j--){
JObjectobj=DBUtils.getJObject("select gameName,sum(counts) counts from gameAnalyse wheretransDate='"+DateUtils.addDay(lastDate,-j)+"' and gameName='"+gameName[i]+"'",Constants.SLAVE_DBNAME);
if(null!=obj){
games.add(obj.getString("counts"));
}else{
games.add("");
}
}
jsts.add(games);
}
for(inti=14;i>0;i--){
jst.add(DateUtils.addDay(lastDate,-i));
}
JSONArraya=newJSONArray();
a.add("英雄联盟");
a.add("地下城与勇士");
a.add("穿越火线");
a.add("多塔");
a.add("QQ飞车");
a.add("QQ炫舞");
a.add("逆战");
a.add("梦幻西游");
a.add("天涯明月刀");
a.add("大话西游2");
a.add("大话西游3");
a.add("剑灵");
a.add("QQ");
a.add("使命召唤");
json.put("transDate",jst);
json.put("gameName",jsts);
json.put("games",gameName);
System.out.println(json.toString());
out.write(json.toString().getBytes(Charset.forName("utf-8")));
out.flush();
}
protectedvoiddoPost(HttpServletRequestrequest, HttpServletResponseresponse)throwsServletException, IOException {
doGet(request,response);
}
}
网友评论