java - 如何使用java(spring boot)以json格式检索完整值(带小数的大整数值)?
问题描述
我在 Spring Boot 中创建了一个休息 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>
SampleApplication.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();
}
}
HelloController.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;
}
}
解决方案
将 BigDecimal 转换为 double 的正确方法是:
((BigDecimal) jsobj.get("price")).doubleValue()
但是,您将 BigDecimal 转换为 double。Double 的范围很广,但即使是这个范围也有限制。当你超过它时,你会遇到转换问题。
我建议的答案是,不要将其转换为双重使用 BigDecimal 以支付您的价格。
推荐阅读
- scala - 如何将 DataFrame 的所有列(带有嵌套的 StructTypes 和嵌套的 ArrayType)转换为 Spark 中的字符串
- c# - 如何在列表中找到日期早于指定日期但所有数据类型都是字符串的条目?
- c# - “/”应用程序文件加载异常中的服务器错误
- javascript - 如何使用 jquery 滑动横幅?
- wordpress - “添加帖子”有时会覆盖以前的帖子 Wordpress
- api - 如何在登台服务器上保持 GraphQL API 和前端同步?
- sql-server - 创建表关系而不将它们链接到主键/唯一
- python - 返回 None 的多边形之间的 OGR 交集方法
- javascript - Is there a way to search for a sub-string inside a string with newline (\n) symbols?
- r - 如何使用 metaMDS() 获得“物种分数”以进行排序?