简体   繁体   English

如何解析HTTP响应参数JAVA?

[英]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 币安ETHUSDT

"lastUpdateId":236998360,"bids":[["88.98000000","2.30400000",[]]..........,"asks":[["89.04000000","16.06458000",[]],....... “lastUpdateId”:236998360, “出价”:[[ “88.98000000”, “2.30400000”,[]] .........., “询问”:[[ “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] Double Binance [] [] = [88.98000000] [2.30400000] [89.04000000] [6.06458000]

How can I extract just Price and Qty from the HTTP/GET response? 如何从HTTP / GET响应中仅提取价格和数量?

If the response is a JSON, use a parser like Jackson and extract the values. 如果响应是JSON,请使用类似Jackson的解析器并提取值。 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. 有很多方法可以做到这一点,包括使用正则表达式或简单地使用String函数。 You'd need to create an ArrayList just before the loop and add each price into it. 您需要在循环之前创建一个ArrayList并将每个价格添加到其中。 I highly recommend that you use the Money class from java 9 rather than a double or a float. 我强烈建议您使用Java 9中的Money类,而不要使用double或float。 See -> https://www.baeldung.com/java-money-and-currency 参见-> https://www.baeldung.com/java-money-and-currency

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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