0

I have a JSON String that I'm trying to parse into an array using the following code:

try {
    String holder = getJSONString(getApplicationContext());
    JSONArray JSONARR= new JSONArray(holder);

    List<datatemp> dataList = new ArrayList<>();
    for (int i = 0; i < JSONARR.length(); i++) {

        JSONObject jsonObj = JSONARR.getJSONObject(i);
        datatemp data = new datatemp();

        data.ID = Integer.parseInt(jsonObj.getString("id"));
        data.Open = Integer.parseInt(jsonObj.getString("Open"));
        data.Close = Integer.parseInt(jsonObj.getString("Close"));
        data.High = Integer.parseInt(jsonObj.getString("High"));
        data.Low = Integer.parseInt(jsonObj.getString("Low"));

        dataList.add(data);
    }
} catch (JSONException e) {
    Log.e("JSON_ERROR", "unexpected JSON exception", e);
}

but I keep getting the following error:

type org.json.JSONObject cannot be converted to JSONArray

The JSON String looks like this:

"data": [
    {
      "id": "0",
      "Open": "1",
      "Close": "1",
      "High": "1",
      "Low": "1"
    },
    {
      "id": "1",
      "Open": "1",
      "Close": "1",
      "High": "1",
      "Low": "1"
    },
    {
      "id": "2",
      "Open": "1",
      "Close": "1",
      "High": "1",
      "Low": "1"
    },
    {
      "id": "3",
      "Open": "1",
      "Close": "1",
      "High": "1",
      "Low": "1"
    },
    {
      "id": "4",
      "Open": "1",
      "Close": "1",
      "High": "1",
      "Low": "1"
    },
    {
      "id": "5",
      "Open": "1",
      "Close": "1",
      "High": "1",
      "Low": "1"
    },
    {
      "id": "6",
      "Open": "1",
      "Close": "1",
      "High": "1",
      "Low": "1"
    },
    {
      "id": "7",
      "Open": "1",
      "Close": "1",
      "High": "1",
      "Low": "1"
    },
    {
      "id": "8",
      "Open": "1",
      "Close": "1",
      "High": "1",
      "Low": "1"
    },
    {
      "id": "9",
      "Open": "1",
      "Close": "1",
      "High": "1",
      "Low": "1"
    },
    {
      "id": "10",
      "Open": "1",
      "Close": "1",
      "High": "1",
      "Low": "1"
    }
  ]
}

I'm trying to parse the data so that later I can display it in a chart. I'm just trying to parse the JSON into a list so that the data can be split later into smaller lists (id,open,close,high,low) but I'm not sure if there is a better method.

2 Answers 2

1

Although i'm missing some data, I can assume what goes wrong:

your JSON is:

{
  "data" : [
    {  
      "id": "0",
       "Open": "1",
       "Close": "1",
       "High": "1",
       "Low": "1"
    }
  ]
}

So your JSON contains an Object that contains an Array of objects. You are trying to create a JSONArray from the root element, which is an Object. JSONArray JSONARR = new JSONArray(holder);

You can solve the problem changing your code navigating from the root to the data property, as follows:

JSONObject jsonObj = new JSONObject(holder);
JSONArray JSONARR = jsonObj.getJSONArray("data");
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it worked, I was scratching my head on this for so long.
-1

You may look at thses links, they also helped me a lot but it's in C#, so you may find same library in java too.

1-Getting 'Cannot cast Newtonsoft.Json.Linq.JObject to Newtonsoft.Json.Linq.JToken' when retrieving items from JSON

2-Read and parse a Json File in C#

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.