在用httpclient获取返回的json数据时,有个值是5000000.0,在用EntityUtils.toString(entity, "utf-8");转换的时候会变成5.0E7,哪位知道怎么处理吗?
String responseMsg = "";
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpget = getHttpGet(url);
try {
httpget.setHeader("Content-Type", "application/xml; charset=utf-8");
httpget.setHeader("User-Agent",
"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36");
CloseableHttpResponse response = httpClient.execute(httpget);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
responseMsg = EntityUtils.toString(entity, "utf-8");
}
} finally {
response.close();
}
httpClient.close();
} catch (Exception e) {
throw new SystemException("webservice请求异常", e);
} finally {
httpget.releaseConnection();
httpClient.close();
// httpClient.getConnectionManager().shutdown();
}
return responseMsg;
jdk自带的和OkHttp包也是同样返回
看到这个,也卡了半天,可以用这种方式实现:
使用com.alibaba.fastjson包先转换为json,再转换为string。
public String toJsonThenToString(String responseMsg){
try{
JSONObject jsonObject=JSON.parseObject(responseMsg);
return JSON.toJSONString(jsonObject);
}catch(Exception e){
return responseMsg;
}
}
如果你一定要转码的话,那就是转码方法里面转换的,那你拿到值的时候再转一遍吧
确实是这样的,但是如果是 EntityUtils.toString(entity, "utf-8"); 该方法转换的,那你也没办法去改他的源码 啊!你可以采用别的方式转码,获取使用反射封装一个方法,将对象转map