user1349407
user1349407

Reputation: 622

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?

Upvotes: 0

Views: 2365

Answers (2)

VarunKrish
VarunKrish

Reputation: 179

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.

Upvotes: 1

fpezzini
fpezzini

Reputation: 794

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

Upvotes: 0

Related Questions