sparker
sparker

Reputation: 1578

Keep Trailing zeros when Parsing Json using jayway JsonPath

I am using jayway json-path - 2.4.0 to parse Json. When Parsing json, if the json contains any double value, its truncating the trailing zeros. For Example, I have a Json String like below.

{"name" : "Sparker" , "value": 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. How to get the result without truncating trailing zeros.

Upvotes: 1

Views: 582

Answers (1)

Michael
Michael

Reputation: 44200

You are not specifying a type - you are just using Object - so it is choosing Double automatically. Double has no concept of trailing zeros: that is an aspect of how a number is displayed.

See What is Returned When?

Upvotes: 1

Related Questions