I have a question for JSON parsing

Asked 2 years ago, Updated 2 years ago, 39 views

Below is the URL to get the 1st batch list of the Bithumb candle chart

https://api.bithumb.com/public/candlestick/ETH/1m

{"status":"0000","data":[[1582464360000,"321000","320900","321000","320900","11.7071"],[],[]]}

I want to get the data How do I parse?

List(string[]) data = ? 

org.json.JSONObject Library in use

json android java

2022-09-21 12:26

2 Answers

    @Test
    public void parseFromString() {
        final String jsonString = "{\"status\":\"0000\",\"data\":[[1582464360000,\"321000\",\"320900\",\"321000\",\"320900\",\"11.7071\"],[],[]]}";
        JSONObject object = new JSONObject(jsonString);

        JSONArray data = (JSONArray) object.get("data");
        Assert.assertEquals(JSONArray.class, data.get(0).getClass());

        JSONArray firstElement = (JSONArray) data.get(0);
        Assert.assertEquals(Long.class, firstElement.get(0).getClass());
        Assert.assertEquals(String.class, firstElement.get(1).getClass());

        JSONArray secondEmptyElement = (JSONArray) data.get(1);
        Assert.assertEquals(0, secondEmptyElement.length());
    }

Just do it like above.

Actually, it is more convenient to use fastxml-jackson or Google-Gson than this.


2022-09-21 12:26

You can approach it in the form below.

jsonStr = "{\"status\":\"0000\",\"data\":[[1582464360000,\"321000\",\"320900\",\"321000\",\"320900\",\"11.7071\"],[],[]]}"
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray jsonArray = jsonObj .getJSONArray("data");


2022-09-21 12:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.