日期:2014-05-19 浏览次数:21006 次
public static String escapeUnicode(String s) {
if (s == null) {
return s;
}
char[] chars = s.toCharArray();
char c;
StringBuffer sb = new StringBuffer();
for (int i = 0; i < chars.length; i++) {
c = chars[i]; // Ignore ascii character
if (c > 0xff) {
sb.append("\\u").append(Integer.toHexString(c));
} else {
sb.append(c);
}
}
return sb.toString();
}
public static void jsonData(List list) {
JSONObject jsonObj = new JSONObject();
JSONArray jsonArray = new JSONArray();
for (Object tmp : list) {
HashMap tplHm = (HashMap) tmp;
Iterator iter = tplHm.keySet().iterator();
JSONObject jObj = new JSONObject();
while (iter.hasNext()) {
Object key = iter.next();
try {
/*判断是否中文再进行转码
if (isChinese(tplHm.get(key).toString())) {
jObj.element(key.toString(), URLEncoder.encode(tplHm.get(key).toString(), "UTF-8"));
} else {
jObj.element(key.toString(), tplHm.get(key).toString());
}
*/
//不判断,直接转码为十六进制
jObj.element(key.toString(), escapeUnicode(tplHm.get(key).toString()));
} catch (Exception e) {
}
}
jsonArray.add(jObj);
}
jsonObj.element("data", jsonArray);
try {
ServletActionContext.getResponse().getWriter().write(String.valueOf(jsonObj));
} catch (IOException e) {
e.printStackTrace();
}
}