我想从不包含双引号的 JSONObject 中提取 JSONArray。
HTTP响应实体如下。
{"asks":[["107.47649000",25.3039]],"bids":[["107.06385000",64.9317]],"isFrozen":"0","seq":298458396}
具体来说,我需要同时提取 107.4764900 和 25.3039。但是,值 25.3039 不包含双引号。
我的代码如下
public static void bid_ask () throws ClientProtocolException, IOException {
String queryArgs = "https://poloniex.com/public?command=returnOrderBook¤cyPair=USDT_ETH&depth=1";
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(queryArgs);
CloseableHttpResponse response = httpClient.execute(post);
HttpEntity responseEntity = response.getEntity();
System.out.println("Poloniex ETHUSDT");
//System.out.println(response.getStatusLine());
if (responseEntity != null) {
try (InputStream stream = responseEntity.getContent()) {
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
JSONObject jsonObj = new JSONObject(line);
JSONArray tokenListBid = jsonObj.getJSONArray("bids");
JSONArray tokenListAsk = jsonObj.getJSONArray("asks");
JSONArray bid_element;
JSONArray ask_element;
double bid[] = new double[2];
double ask[] = new double[2];
System.out.println("Poloniex Bid");
if (tokenListBid != null) {
for (int i=0;i<2;i++){
bid_element = tokenListBid.getJSONArray(i);
bid[i]=bid_element.getBigDecimal(i).doubleValue();
System.out.println(bid[i]);
}
}
} //end while()
}
}
结果显示如下
Poloniex 出价 107.06385
线程“主”org.json.JSONException 中的异常:找不到 JSONArray[1]。
谢谢。