gpt4 book ai didi

java - 如何在 Spring Boot 上返回 JSONObject?

转载 作者:行者123 更新时间:2023-12-02 01:53:37 24 4
gpt4 key购买 nike

我有一个 Web 服务正在调用另一个 Web 服务:

String restServiceUrl = "https://api.coinmarketcap.com/v2/ticker/?start" + start + "&limit=" + limit + "&sort=" + sort;
String coinmarketcapJSONString = new RestTemplate().getForObject(restServiceUrl, String.class);
JSONObject coinmarketcapJSONObject = new JSONObject(coinmarketcapJSONString).getJSONObject("data");
JSONArray coinmarketcapJSONArray = coinmarketcapJSONObject.names();

我的coinmarketcapJSONObject看起来像这样:

{"1":{"symbol":"BTC","circulating_supply":1.7300962E7,"last_updated":1538495424,"total_supply":1.7300962E7,"name":"Bitcoin","max_supply":2.1E7,"rank":1,"id":1,"website_slug":"bitcoin","quotes":{"USD":{"percent_change_1h":-0.1,"market_cap":1.13449548429E11,"percent_change_24h":-0.12,"price":6557.41272818,"volume_24h":3.85360400868166E9,"percent_change_7d":2.1}}},"1027":{"symbol":"ETH","circulating_supply":1.02322624E8,"last_updated":1538495440,"total_supply":1.02322624E8,"name":"Ethereum","max_supply":null,"rank":2,"id":1027,"website_slug":"ethereum","quotes":{"USD":{"percent_change_1h":-0.21,"market_cap":2.3260421917E10,"percent_change_24h":-0.19,"price":227.32432918,"volume_24h":1.49265685549155E9,"percent_change_7d":7.69}}}}

我想这样返回:

return new ResponseEntity<JSONObject>(coinmarketcapJSONObject, HttpStatus.OK);

但是当我这样做时,我一直遇到错误:

"No converter found for return value of type: class org.json.JSONArray"

enter image description here

我在网上看到我必须安装 Jackson 依赖项。我安装了它们,但没有任何改变。

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.7</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.7</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
<version>2.9.7</version>
</dependency>

最佳答案

您可以将其作为字符串返回。

return new ResponseEntity<String>(coinmarketcapJSONObject.toString(), HttpStatus.OK);

关于java - 如何在 Spring Boot 上返回 JSONObject?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52612131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com