第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

調(diào)用外部API并解析JSON對(duì)象

調(diào)用外部API并解析JSON對(duì)象

慕妹3146593 2023-08-04 15:49:45
我正在嘗試從外部 API 獲取 JSON 并解析它,以便我可以訪(fǎng)問(wèn) JSON 對(duì)象值,以避免我不需要的值(字段)。我已經(jīng)研究了一點(diǎn) JSONOBject 庫(kù)并解析了對(duì)象,但是,感覺(jué)我做了太多的硬編碼并且根本沒(méi)有正確執(zhí)行,非常感謝反饋。用戶(hù)輸入后輸出結(jié)果:發(fā)送“GET”請(qǐng)求到 URL: https: //api.coindesk.com/v1/bpi/currentprice/(用戶(hù)輸入貨幣)數(shù)據(jù)在 UTC 時(shí)間/日期獲取:2019 年 9 月 17 日 22:51:00 UTC 說(shuō)明:烏克蘭格里夫納匯率浮動(dòng):253737.6633import org.json.JSONObject;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;public class App {    public static void main(String[] args) throws IOException {        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));        System.out.println("Please enter the currency you would like to get BTC current rate for");        String userInput = in.readLine();        try {            String url = "https://api.coindesk.com/v1/bpi/currentprice/" + userInput;            URL obj = new URL(url);            // HttpURLConnection instance is making a request            //openConnection() method of URL class opens the connection to specified URL            HttpURLConnection con = (HttpURLConnection) obj.openConnection();            // saving the response code from the request            int responseCode = con.getResponseCode();            System.out.println("\nSending 'GET' request to URL : " + url);            System.out.println("---------------------------------------------");            System.out.println("Response Code from the HTTP request : " + responseCode);            System.out.println("---------------------------------------------");            }
查看完整描述

1 回答

?
飲歌長(zhǎng)嘯

TA貢獻(xiàn)1951條經(jīng)驗(yàn) 獲得超3個(gè)贊

ObjectMapper我編寫(xiě)了一個(gè)示例代碼,通過(guò)使用將響應(yīng) json 字符串轉(zhuǎn)換為 POJO 來(lái) 演示我在評(píng)論中所說(shuō)的內(nèi)容。


首先,創(chuàng)建一個(gè)類(lèi),說(shuō)CoinDeskResponse是存儲(chǔ)轉(zhuǎn)換結(jié)果。


public class CoinDeskResponse {

    private TimeInfo time;

    private String disclaimer;

    private BpiInfo bpi;

    //general getters and setters

}


class TimeInfo {

    private String updated;

    private String updatedISO;

    //general getters and setters

}


class BpiInfo {

    private String code;

    private String symbol;

    private String rate;

    private String description;


    @JsonProperty("rate_float")

    private String rateFloat;

    //general getters and setters

}

接下來(lái),創(chuàng)建ObjectMapper響應(yīng)并將其轉(zhuǎn)換為CoinDeskResponsePOJO。然后就可以通過(guò)操作該對(duì)象來(lái)獲取所需的數(shù)據(jù)。


    String responseStr = "{\"time\":{\"updated\":\"Sep 18, 2013 17:27:00 UTC\",\"updatedISO\":\"2013-09-18T17:27:00+00:00\"},\"disclaimer\":\"This data was produced from the CoinDesk Bitcoin Price Index. Non-USD currency data converted using hourly conversion rate from openexchangerates.org\",\"bpi\":{\"code\":\"USD\",\"symbol\":\"$\",\"rate\":\"126.5235\",\"description\":\"United States Dollar\",\"rate_float\":126.5235}}";

    ObjectMapper mapper = new ObjectMapper();

    try {

        CoinDeskResponse coinDeskResponse = mapper.readValue(responseStr, CoinDeskResponse.class);


        System.out.println(coinDeskResponse.getTime().getUpdated());

        System.out.println(coinDeskResponse.getBpi().getDescription());

        System.out.println(coinDeskResponse.getBpi().getRateFloat());

    } catch (IOException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

    }

控制臺(tái)輸出:


數(shù)據(jù)在 UTC 時(shí)間/日期獲取:2013 年 9 月 18 日 17:27:00 UTC

描述:美元

浮動(dòng)匯率:126.5235


查看完整回答
反對(duì) 回復(fù) 2023-08-04
  • 1 回答
  • 0 關(guān)注
  • 163 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)