user1349407
user1349407

Reputation: 622

Extract JSONArray from JSONObject which does not contain double quote

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&currencyPair=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.

Upvotes: -1

Views: 70

Answers (2)

Joakim Danielson
Joakim Danielson

Reputation: 51821

Treat the inner array as an array of Object and typecast them as needed.
Here is simplified example of parsing the "asks" part

JSONObject jsonObj = new JSONObject(line);
JSONArray asks = jsonObj.getJSONArray("asks").getJSONArray(0);
Double ask = 0.0;
for (Object o : asks) {
    if (o instanceof String){
        ask = Double.valueOf((String)o);
    } else {
        ask = (Double)o;
    }
    //do something with ask
}

Update
Here is another way to access the data

JSONObject jsonObj = new JSONObject(line);
SONArray asks = jsonObj.getJSONArray("asks").getJSONArray(0);

Double price = Double.valueOf(asks.getString(0));
Double qty = Double.valueOf(asks.getDouble(1));

System.out.printf("Ask price: %.6f, quantity %.4f", price, qty);

This is the string I used to test my code

String json = "{\"asks\":[[\"107.47649000\",25.3039]],\"bids\":[[\"107.06385000\",64.9317]],\"isFrozen\":\"0\",\"seq\":298458396}";

and this is the result I got

Ask price: 107.476490, quantity 25.3039

Upvotes: 1

ZhaoGang
ZhaoGang

Reputation: 4915

[["107.06385000",64.9317]] is a 1*2 array, tokenListBid is refering to it. So you cann't use tokenListBid.getJSONArray(1); to index it.

Upvotes: -1

Related Questions