最近使用老系统copy的功能方法中,获取到的返回json中,发现loginname,srcname,等名字字段的属性,很明显有乱码,如:"%E5%A4%A7%E6%96%B0%E5%8D%8E%E4%BC%9A%E5%B1%95"
等字段,
通过json转换成对象之后,发现涉及到中文字段的,哪怕提前设置了UTF-8格式,返回结果依然会是乱码,
先通过如下方式进行解决
看代码:
HttpClient hc = new HttpClient();
PostMethod pm = new PostMethod(getHttpUrl(request,platformAuthUrl));
hc.getParams().setContentCharset("UTF-8");
pm.setParameter("ticket", ticket);
hc.executeMethod(pm);
String in=pm.getResponseBodyAsString();
返回结果中,包含:
{dataControlList:"0101%211%211",delegationList:"",departAB:"HMIC",deptId:1,deptName:"%E5%A4%A7%E6%96%B0%E5%8D%8E%E4%BC%9A%E5%B1%95",deptNo:"0101",excludeCorpList:"",id:100,loginName:"admin",platformCallbackUrl:"http%3A%2F%2Fplatform%2Fnoright.jsp",roles:"171",srcDeptId:1,srcDeptNo:"0101",srcId:100,srcLoginName:"admin",srcUserName:"%28T%29%E8%B6%85%E7%BA%A7%E7%AE%A1%E7%90%86%E5%91%98",srcUserNo:"100",superAdmin:true,systemId:10017,userAB:"AD",userNo:"100",userdataControlList:"",username:"%28T%29%E8%B6%85%E7%BA%A7%E7%AE%A1%E7%90%86%E5%91%98"}
通过对%E5%A4%A7%E6%96%B0%E5%8D%8E%E4%BC%9A%E5%B1%95进行解析
public class testString {
public static void main(String[] args) throws UnsupportedEncodingException {
String ss = "%E5%A4%A7%E6%96%B0%E5%8D%8E%E4%BC%9A%E5%B1%95";
String s1 = URLDecoder.decode(ss,"UTF-8");
System.out.println(s1);
String s3 = "0101%211%211";
System.out.println(URLDecoder.decode(s3,"UTF-8"));
String s4 = "%28T%29%E8%B6%85%E7%BA%A7%E7%AE%A1%E7%90%86%E5%91%98";
System.out.println(URLDecoder.decode(s4,"UTF-8"));
}
}
通过URLDecoder.decode(ss,"UTF-8");
发现可以获取中文,完美解决