java - 使用 jayway JsonPath 解析 Json 时保持尾随零
问题描述
我正在使用 jayway json-path - 2.4.0 来解析 Json。解析 json 时,如果 json 包含任何 double 值,则它会截断尾随零。例如,我有一个如下所示的 Json 字符串。
{“名称”:“火花”,“价值”:60.10}
我的代码:
package com.test;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import java.math.BigDecimal;
public class TestClas {
public static void main(String[] args) {
String value = "{\"name\" : \"Sparker\" , \"value\": 60.10}";
DocumentContext docCtx = JsonPath.parse(value);
JsonPath jsonPath = JsonPath.compile("$.value");
Object result = docCtx.read(jsonPath);
System.out.println(result);
}
}
它只打印 60.1,但我需要 60.10。如何在不截断尾随零的情况下获得结果。
解决方案
您没有指定类型 - 您只是在使用Object
- 所以它会Double
自动选择。Double
没有尾随零的概念:这是数字显示方式的一个方面。
请参阅什么时候返回?
推荐阅读
- reactjs - 比较来自 Firestore 的文档 ID?
- reactjs - 无法在反应中将前端表单值发送到后端 api,expressjs 应用程序?
- php - Laravel 7 - 如何使用除用户表之外的其他表进行登录?
- swift - 长按应用程序图标时如何重新创建触觉反馈振动
- javascript - 为什么来自 Firestore 的聊天消息会显示在“群组”中?
- reactjs - 如何使用 react-relax-wrapper 停止视差(React,Rellax)
- vba - 在 Ms 访问中创建一个建议和过滤的组合框
- java - 如何在数组中找到int元素的索引
- mysql - 存储下载信息的 MySQL EER 数据库模型?
- node.js - 在循环中运行查询时乏味的失去连接