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 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable