简体   繁体   中英

How to parse HTTP response argument JAVA?

I am now working on making get current order book of crypto currencies.

my codes are as shown below.

    public static void bid_ask () {
    HttpClient client = HttpClientBuilder.create().build();
    HttpGet request = new HttpGet("https://api.binance.com//api/v1/depth?symbol=ETHUSDT");
    System.out.println("Binance ETHUSDT");

    try {
        HttpResponse response = client.execute(request);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            try (InputStream stream = entity.getContent()) {
                BufferedReader reader =
                        new BufferedReader(new InputStreamReader(stream));
                String line;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

result of this code is as follows..

Binance ETHUSDT

"lastUpdateId":236998360,"bids":[["88.98000000","2.30400000",[]]..........,"asks":[["89.04000000","16.06458000",[]],.......

What I want to make is a kind of price array..

Double Binance[][] = [88.98000000][2.30400000][89.04000000][6.06458000]

How can I extract just Price and Qty from the HTTP/GET response?

If the response is a JSON, use a parser like Jackson and extract the values. This can be then added to array.

This link will help you.

You will need to parse those values out of the variable line:
There are many ways to do that including using regular expressions or simply using String functions. You'd need to create an ArrayList just before the loop and add each price into it. I highly recommend that you use the Money class from java 9 rather than a double or a float. See -> https://www.baeldung.com/java-money-and-currency

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM