java从url获取json数组

68bkxrlz  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(434)

我们的中央银行以多种方式提供货币汇率。例如,一种日期货币很容易获得: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 在代码行后返回“nullsession”

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

尽管这个json在webrowser中没有任何问题。它以方括号[作为数组]开始。
你见过并解决过这样的问题吗?
请从我的idempiere开发ide中找到以下异常堆栈跟踪。
pm org.compiere.process.svr进程严重:nullsession java.lang.nosuchfielderror:nullsession at sun.security.ssl.clienthandshaker.getkickstartmessage(clienthandshaker)。java:1343)在sun.security.ssl.handshaker.kickstart(握手器。java:1117)在sun.security.ssl.sslsocketimpl.kickstartshandshake(sslsocketimpl。java:1500)在sun.security.ssl.sslsocketimpl.performitialhandshake(sslsocketimpl。java:1367)在sun.security.ssl.sslsocketimpl.startAndShake(sslsocketimpl。java:1416)在sun.security.ssl.sslsocketimpl.startAndShake(sslsocketimpl。java:1400)在sun.net。www.protocol.https.httpsclient.afterconnect(httpsclient。java:559)在太阳网。www.protocol.https.abstractdelegatehttpsurlconnection.connect(abstractdelegatehttpsurlconnection。java:185)在sun.net。www.protocol.http.httpurlconnection.getinputstream0(httpurlconnection。java:1570)在sun.net。www.protocol.http.httpurlconnection.getinputstream(httpurlconnection。java:1498)在java.net.httpurlconnection.getresponsecode(httpurlconnection。java:480)在sun.net。www.protocol.https.httpsurlconnectionimpl.getresponsecode(httpsurlconnectionimpl。java:352)在one.stabilis.utils.jsonutils.readjsontostring(jsonutils。java:75)在one.stabilis.utils.jsonutils.readarrayofexchangeratestable(jsonutils。java:108)在一个.stabilis.utils.jsonutils.publishedondatetable(jsonutils。java:55)在one.stabilis.utils.jsonutils.getpublishedondatetable(jsonutils。java:32)在one.stabilis.impconvrates.importcurrencyrates.doit(importcurrencyrates。java:84)在org.compiere.process.svrprocess.process(svrprocess。java:201)在org.compiere.process.svrprocess.startprocess(svrprocess。java:147)在org.adempiere.util.processutil.startjavaprocess(processutil。java:172)在org.adempiere.util.processutil.startjavaprocess(processutil。java:139)在org.adempiere.util.processutil.startjavaprocess(processutil。java:128)在org.compiere.interfaces.impl.serverbean.process(serverbean。java:78)在org.compiere.apps.abstractprocessctl.startprocess(abstractprocessctl。java:429)在org.compiere.apps.abstractprocessctl.run(abstractprocessctl。java:234)在org.adempiere.webui.apps.wprocessctl.process(wprocessctl。java:197)在org.adempiere.webui.apps.abstractprocessdialog$processdialogrunnable.dorun(abstractprocessdialog。java:1083)在org.adempiere.util.contextrunnable.run(contextrunnable。java:38)在org.adempiere.webui.apps.desktoprunnable.run(desktoprunnable。java:40)在java.util.concurrent.executors$runnableadapter.call(executors。java:511)在java.util.concurrent.futuretask.run(futuretask。java:266)在java.util.concurrent.scheduledthreadpoolexecutor$scheduledfuturetask.access$201(scheduledthreadpoolexecutor。java:180)在java.util.concurrent.scheduledthreadpoolexecutor$scheduledfuturetask.run(scheduledthreadpoolexecutor。java:293)在java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor。java:1149)在java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor。java:624)在java.lang.thread.run(线程。java:748)

hzbexzde

hzbexzde1#

您可以将响应Map为 List<JsonNode> 或者 List<CustomMapperObject> .
因为响应本身是一个数组而不是一个对象,所以可以这样做

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

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

我建议你去看看Jackson图书馆,作为参考

相关问题