如何解析HTTP响应参数JAVA?

时间:2018-12-11 10:57:12

标签: java http parsing

我正在致力于获取当前的加密货币订单。

我的代码如下所示。

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

此代码的结果如下。

Binance ETHUSDT

“ lastUpdateId”:236998360,“出价”:[[“ 88.98000000”,“ 2.30400000”,[]] ..........,“请求”:[[“ 89.04000000”, “ 16.06458000”,[]],.......

我想做的是一种价格数组。

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

如何从HTTP / GET响应中仅提取价格和数量?

2 个答案:

答案 0 :(得分:1)

如果响应是JSON,请使用类似Jackson的解析器并提取值。然后可以将其添加到数组。

link将为您提供帮助。

答案 1 :(得分:0)

您将需要从变量行中解析这些值:
有很多方法可以做到这一点,包括使用正则表达式或简单地使用String函数。您需要在循环之前创建一个ArrayList并将每个价格添加到其中。我强烈建议您使用Java 9中的Money类,而不要使用double或float。参见-> https://www.baeldung.com/java-money-and-currency

相关问题