I want to extract JSONArray from JSONObject which does not contain double quote.
HTTP response entity as follows.
{"asks":[["107.47649000",25.3039]],"bids":[["107.06385000",64.9317]],"isFrozen":"0","seq":298458396}
In specific I need to extract both 107.4764900 and 25.3039. However, the value 25.3039 does not contain double quote.
my codes are below
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()
}
}
the result shows like this
Poloniex Bid 107.06385
Exception in thread "main" org.json.JSONException: JSONArray[1] not found.
thanks.
Treat the inner array as an array of Object and typecast them as needed.
Here is simplified example of parsing the "asks" part
Update
Here is another way to access the data
This is the string I used to test my code
and this is the result I got