简体   繁体   English

使用 jayway JsonPath 解析 Json 时保持尾随零

[英]Keep Trailing zeros when Parsing Json using jayway JsonPath

I am using jayway json-path - 2.4.0 to parse Json.我正在使用 jayway json-path - 2.4.0 来解析 Json。 When Parsing json, if the json contains any double value, its truncating the trailing zeros.解析 json 时,如果 json 包含任何双精度值,则截断尾随零。 For Example, I have a Json String like below.例如,我有一个像下面这样的 Json 字符串。

{"name" : "Sparker" , "value": 60.10} {“名称”:“火花”,“价值”:60.10}

And my Code:还有我的代码:

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);
  }
}

And it just prints 60.1, but I need 60.10.它只打印 60.1,但我需要 60.10。 How to get the result without truncating trailing zeros.如何在不截断尾随零的情况下获得结果。

You are not specifying a type - you are just using Object - so it is choosing Double automatically.您没有指定类型 - 您只是在使用Object - 所以它会自动选择Double Double has no concept of trailing zeros: that is an aspect of how a number is displayed . Double没有尾随零的概念:这是数字显示方式的一个方面。

See What is Returned When?请参阅什么时候返回?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM