fastjson FastJSON parse will conver float or double to bigdecimal.

gcuhipw9  于 2021-11-27  发布在  Java
关注(0)|答案(0)|浏览(203)

source code.

String data = "{\n" +
                "    \"address\": {\n" +
                "        \"addressLine\": \"浙江省杭州市余杭区溪望路靠近阿里巴巴淘宝城2期\", \n" +
                "        \"city\": \"杭州市\", \n" +
                "        \"cityCode\": \"0571\", \n" +
                "        \"district\": \"余杭区\", \n" +
                "        \"province\": \"浙江省\", \n" +
                "        \"road\": \"溪望路\"\n" +
                "    }, \n" +
                "    \"coords\": {\n" +
                "        \"accuracy\": 29, \n" +
                "        \"altitude\": 0, \n" +
                "        \"bearing\": 0, \n" +
                "        \"latitude\": 30.278379, \n" +
                "        \"longitude\": 120.029034, \n" +
                "        \"speed\": 0\n" +
                "    }, \n" +
                "    \"errorCode\": 0\n" +
                "}";
        JSONObject object = JSONObject.parseObject(data);

you should first test number precision first, choose right number precision. how to choose right number type ? maybe the following works:

BigDecimal  number = new BigDecimal("0.0");
                String value = number.toString();
                if(value.equals(Double.toString(number.doubleValue()))){
                     //use double type, which is enouch
                }else {
                    //bigdecimal
                }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题