【问题标题】:Keep Trailing zeros when Parsing Json using jayway JsonPath使用 jayway JsonPath 解析 Json 时保持尾随零
【发布时间】:2020-07-03 18:21:46
【问题描述】:

我正在使用 jayway json-path - 2.4.0 来解析 Json。解析 json 时,如果 json 包含任何 double 值,则它会截断尾随零。例如,我有一个如下所示的 Json 字符串。

{"name" : "Sparker" , "value": 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。如何在不截断尾随零的情况下获得结果。

【问题讨论】:

    标签: java json jsonpath jayway


    【解决方案1】:

    你没有指定一个类型——你只是在使用Object——所以它会自动选择DoubleDouble 没有尾随零的概念:这是数字如何显示的一个方面。

    What is Returned When?

    【讨论】:

    • 我想让我的课程通用。我想解析返回不同对象(字符串、整数、列表、地图等)的不同 Json 路径。这就是使用对象的原因。
    猜你喜欢
    • 2020-06-22
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    相关资源
    最近更新 更多