EverSpring007 2017-12-04 08:51 采纳率: 0%
浏览 3861
已采纳

httpclient获取返回的json串会自动转换成科学计数法

在用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包也是同样返回

  • 写回答

3条回答 默认 最新

  • Hello_CYY 2018-06-13 09:50
    关注

    看到这个,也卡了半天,可以用这种方式实现:
    使用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;
    }
    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 已采纳回答 8月9日

悬赏问题

  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示