如何使用java(spring boot)以json格式检索全值(带小数的大整数值)?

问题描述 投票:2回答:2

我在spring boot中创建了一个rest API,它消耗第三方API响应并返回响应。

挑战:第三方API响应{“price”:123456789123456789123456789.05}

我想从单个对象获得“价格”,因此它将按预期返回123456789123456789123456789.05

预期产出:123456789123456789123456789.05

实际产量:1.2345678912345679e + 26

注意:

实际输出将以指数格式给出,但我想要实际数据而不是指数。

代码:pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>sample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sample</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- tag::actuator[] -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- end::actuator[] -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

sample application.Java

package com.example.sample;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;


@SpringBootApplication
public class SampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(SampleApplication.class, args);
    }

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

hello controller.Java

package com.example.sample;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

@RestController
public class HelloController {
 @Autowired
 RestTemplate restTemplate;

 @RequestMapping("/")
 public double index() throws Exception {
  HttpHeaders headers = new HttpHeaders();
  headers.set("token", "rtp");
  HttpEntity entity = new HttpEntity < String > ("parameters", headers);
  String END_Point_Test_API = "http://localhost:8080/api/test";
  ResponseEntity < String > responseData = restTemplate.exchange(END_Point_Test_API, HttpMethod.GET, entity, String.class);
  //Third party response {"price":123456789123456789123456789.05}
  System.out.println(responseData.getBody());
  String data = responseData.getBody();

  JSONParser parser = new JSONParser();
  JSONObject jsobj = (JSONObject) parser.parse(data);
  //double price =(double)jsobj.get("price");
  double price = Double.parseDouble(String.valueOf(jsobj.get("price")));
  return price;

 }

}
java json spring-boot double
2个回答
2
投票

将BigDecimal转换为double的正确方法是:

((BigDecimal) jsobj.get("price")).doubleValue()

但是,您将BigDecimal转换为double。双倍范围广泛,但即使这个范围也有限制。超过它时,您将遇到转换问题。

我建议的答案是,不要将它转换为双倍使用BigDecimal为您的价格。


0
投票

为了说清楚:一旦使用double,你就得到了实际值的近似值,没有明确的精度,一切都只是一些不完美的修复。

BigDecimal price = (BigDecimal) jsobj.get("price");
System.out.println(price.toPlainString());

这可能也在提供的链接中提到。 toPlainString将避免使用科学的电子符号。

© www.soinside.com 2019 - 2024. All rights reserved.