从URL中使用Java获取JSON数组。

huangapple go评论87阅读模式
英文:

get Json Array from URL by java

问题

以下是您提供的内容的翻译:

我们的中央银行提供多种方式的货币汇率。例如,可以轻松获取特定日期的一种货币汇率:http://api.nbp.pl/api/exchangerates/rates/a/usd/2020-08-20?format=json
(它以典型的 JSON 形式开始,使用简单的大括号 {)

但是另一个表格 - 每种货币在特定日期的汇率:http://api.nbp.pl/api/exchangerates/tables/a/2020-08-20?format=json,在代码行

if (httpURLConnection.getResponseCode() == okRespCode) { ...

后返回了 'nullSession',尽管这个 JSON 在网络浏览器中呈现没有任何问题。它以方括号 [ 开头,表示一个数组。

您是否曾遇到并解决过这样的问题?

请在下面找到来自我的 iDempiere 开发 IDE 的异常堆栈跟踪。

PM org.compiere.process.SvrProcess process SEVERE: nullSession java.lang.NoSuchFieldError: nullSession at
sun.security.ssl.ClientHandshaker.getKickstartMessage(ClientHandshaker.java:1343)
at sun.security.ssl.Handshaker.kickstart(Handshaker.java:1117) at
sun.security.ssl.SSLSocketImpl.kickstartHandshake(SSLSocketImpl.java:1500)
at
sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1367)
at
sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1416)
at
sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1400)
at
sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
at
sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
at
sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1570)
at
sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1498)
at
java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at
sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:352)
at one.stabilis.utils.JsonUtils.readJsonToString(JsonUtils.java:75)
at
one.stabilis.utils.JsonUtils.readArrayOfExchangeRatesTable(JsonUtils.java:108)
at
one.stabilis.utils.JsonUtils.publishedOnDateTable(JsonUtils.java:55)
at
one.stabilis.utils.JsonUtils.getPublishedOnDateTable(JsonUtils.java:32)
at
one.stabilis.impconvrates.ImportCurrencyRates.doIt(ImportCurrencyRates.java:84)
at org.compiere.process.SvrProcess.process(SvrProcess.java:201) at
org.compiere.process.SvrProcess.startProcess(SvrProcess.java:147) at
org.adempiere.util.ProcessUtil.startJavaProcess(ProcessUtil.java:172)
at
org.adempiere.util.ProcessUtil.startJavaProcess(ProcessUtil.java:139)
at
org.adempiere.util.ProcessUtil.startJavaProcess(ProcessUtil.java:128)
at
org.compiere.interfaces.impl.ServerBean.process(ServerBean.java:78)
at
org.compiere.apps.AbstractProcessCtl.startProcess(AbstractProcessCtl.java:429)
at
org.compiere.apps.AbstractProcessCtl.run(AbstractProcessCtl.java:234)
at org.adempiere.webui.apps.WProcessCtl.process(WProcessCtl.java:197)
at
org.adempiere.webui.apps.AbstractProcessDialog$ProcessDialogRunnable.doRun(AbstractProcessDialog.java:1083)
at org.adempiere.util.ContextRunnable.run(ContextRunnable.java:38)
at
org.adempiere.webui.apps.DesktopRunnable.run(DesktopRunnable.java:40)
at
java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266) at
java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at
java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

英文:

Our Central Bank provides currency exchange rates in many ways. Ex. one currency on date is easy to get: http://api.nbp.pl/api/exchangerates/rates/a/usd/2020-08-20?format=json
(It begins with simple brace { as typical Json)

but another table - each currency on date: http://api.nbp.pl/api/exchangerates/tables/a/2020-08-20?format=json returns 'nullSession' after line of the the code

	if (httpURLConnection.getResponseCode() == okRespCode) { ...

despite the fact that this Json is presented in webrowser without any problem. It begins with square bracket [ as an array.

Have you ever met and solved such an issue?

Please find below exception stack trace from my iDempiere development IDE.

> PM org.compiere.process.SvrProcess process SEVERE: nullSession java.lang.NoSuchFieldError: nullSession at
> sun.security.ssl.ClientHandshaker.getKickstartMessage(ClientHandshaker.java:1343)
> at sun.security.ssl.Handshaker.kickstart(Handshaker.java:1117) at
> sun.security.ssl.SSLSocketImpl.kickstartHandshake(SSLSocketImpl.java:1500)
> at
> sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1367)
> at
> sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1416)
> at
> sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1400)
> at
> sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
> at
> sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
> at
> sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1570)
> at
> sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1498)
> at
> java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
> at
> sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:352)
> at one.stabilis.utils.JsonUtils.readJsonToString(JsonUtils.java:75)
> at
> one.stabilis.utils.JsonUtils.readArrayOfExchangeRatesTable(JsonUtils.java:108)
> at
> one.stabilis.utils.JsonUtils.publishedOnDateTable(JsonUtils.java:55)
> at
> one.stabilis.utils.JsonUtils.getPublishedOnDateTable(JsonUtils.java:32)
> at
> one.stabilis.impconvrates.ImportCurrencyRates.doIt(ImportCurrencyRates.java:84)
> at org.compiere.process.SvrProcess.process(SvrProcess.java:201) at
> org.compiere.process.SvrProcess.startProcess(SvrProcess.java:147) at
> org.adempiere.util.ProcessUtil.startJavaProcess(ProcessUtil.java:172)
> at
> org.adempiere.util.ProcessUtil.startJavaProcess(ProcessUtil.java:139)
> at
> org.adempiere.util.ProcessUtil.startJavaProcess(ProcessUtil.java:128)
> at
> org.compiere.interfaces.impl.ServerBean.process(ServerBean.java:78)
> at
> org.compiere.apps.AbstractProcessCtl.startProcess(AbstractProcessCtl.java:429)
> at
> org.compiere.apps.AbstractProcessCtl.run(AbstractProcessCtl.java:234)
> at org.adempiere.webui.apps.WProcessCtl.process(WProcessCtl.java:197)
> at
> org.adempiere.webui.apps.AbstractProcessDialog$ProcessDialogRunnable.doRun(AbstractProcessDialog.java:1083)
> at org.adempiere.util.ContextRunnable.run(ContextRunnable.java:38)
> at
> org.adempiere.webui.apps.DesktopRunnable.run(DesktopRunnable.java:40)
> at
> java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
> at java.util.concurrent.FutureTask.run(FutureTask.java:266) at
> java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
> at
> java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
> at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
> at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
> at java.lang.Thread.run(Thread.java:748)

答案1

得分: 2

你可以将响应映射为 List<JsonNode> 或者 List<CustomMapperObject>

由于响应本身是一个数组而不是一个对象,你可以像这样处理

String responseJson = "一些对象的数组";
List<CustomMapperObject> rates = objectMapper.readValue(responseJson, 
             new ParameterizedTypeReference<List<CustomMapperObject>>() {
});

或者

ResponseEntity<List<CustomMapperObject>> responseObject = 
                       restTemplate.exchange(uri.toUriString(), HttpMethod.GET,
			           httpEntity,
			           new ParameterizedTypeReference<List<CustomMapperObject>>() {
			           });

我建议参考 Jackson Library 进行了解。

英文:

You can either map the response as List<JsonNode> or List<CustomMapperObject>.

Since the response is itself an array rather than an object, you can do something like this

String responseJson = "some array of objects";
List<CustomMapperObject> rates = objectMapper.readValue(responseJson, 
             ParameterizedTypeReference<List<CustomMapperObject>>() {
});

or

ResponseEntity<List<CustomMapperObject>> responseObject = 
                       restTemplate.exchange(uri.toUriString(), HttpMethod.GET,
			           httpEntity,
			           new ParameterizedTypeReference<List<CustomMapperObject>>() {
			           });

I would recommend to have a look at the Jackson Library for reference

huangapple
  • 本文由 发表于 2020年8月22日 04:41:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63529812.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定