亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

調用外部API并解析JSON對象

調用外部API并解析JSON對象

慕妹3146593 2023-08-04 15:49:45
我正在嘗試從外部 API 獲取 JSON 并解析它,以便我可以訪問 JSON 對象值,以避免我不需要的值(字段)。我已經研究了一點 JSONOBject 庫并解析了對象,但是,感覺我做了太多的硬編碼并且根本沒有正確執行,非常感謝反饋。用戶輸入后輸出結果:發送“GET”請求到 URL: https: //api.coindesk.com/v1/bpi/currentprice/(用戶輸入貨幣)數據在 UTC 時間/日期獲?。?019 年 9 月 17 日 22:51:00 UTC 說明:烏克蘭格里夫納匯率浮動: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 回答

?
飲歌長嘯

TA貢獻1951條經驗 獲得超3個贊

ObjectMapper我編寫了一個示例代碼,通過使用將響應 json 字符串轉換為 POJO 來 演示我在評論中所說的內容。


首先,創建一個類,說CoinDeskResponse是存儲轉換結果。


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

}

接下來,創建ObjectMapper響應并將其轉換為CoinDeskResponsePOJO。然后就可以通過操作該對象來獲取所需的數據。


    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();

    }

控制臺輸出:


數據在 UTC 時間/日期獲?。?013 年 9 月 18 日 17:27:00 UTC

描述:美元

浮動匯率:126.5235


查看完整回答
反對 回復 2023-08-04
  • 1 回答
  • 0 關注
  • 154 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號