首页 > 解决方案 > 使用 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。如何在不截断尾随零的情况下获得结果。

标签: javajsonjsonpathjayway

解决方案


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

请参阅什么时候返回?


推荐阅读