user3686811
user3686811

Reputation: 137

How to get data from a very big JSON array?

I am using an API for a game and it returns a very big JSON object. The problem is when i try to access an integer in it, it is not working and showing a part of JSON Object in logcat with orange text, system.err. How can i access any value i want in this array?

The code and the structure of the json object is shown bellow.

private class getGame extends AsyncTask<Integer, Integer, String>{

        Context context;
        private getGame(Context context) {
            this.context = context.getApplicationContext();
        }

        String response;


        @Override
        protected String doInBackground(Integer... params) {
            HttpClient client = new DefaultHttpClient();
            String geturl = "https://eu.api.pvp.net/api/lol/tr/v1.3/game/by-summoner/1795120/recent?api_key=cea2196c-6a12-474f-8f6d-52ad5e612cbc";
            HttpGet get = new HttpGet(geturl);
            HttpResponse responseGet = null;
            try {
                responseGet = client.execute(get);
                HttpEntity resEntityGet = responseGet.getEntity();
                response = EntityUtils.toString(resEntityGet);
                JSONObject jsonObj = new JSONObject(response);
                JSONObject gamesObj = jsonObj.getJSONObject("games");
                JSONObject game0 = jsonObj.getJSONObject("0");
                Log.e("test", ""+game0.getInt("gameId"));
                return "";

            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }

This is the structure of Json object and logcat warnings and example jsonobject is here

Upvotes: 0

Views: 2380

Answers (2)

Hariharan
Hariharan

Reputation: 24853

Try this..

{                // JSONObject
  "games": [     // JSONArray

Change this

JSONObject gamesObj = jsonObj.getJSONObject("games");

to

JSONArray gamesObj = jsonObj.getJSONArray("games").getJSONObject(0).getJSONArray("fellowPlayers");
JSONObject game0 = jsonObj.getJSONObject(0);

For more clarification

JSONObject jsonObj = new JSONObject(response);
JSONArray gamesarray = jsonObj.getJSONArray("games");
JSONObject game0 = gamesarray.getJSONObject(0);
JSONArray fellowarray = game0.getJSONArray("fellowPlayers");
JSONObject fellow0 = fellowarray.getJSONObject(0);
Log.e("test", ""+fellow0.getInt("gameId"));

Upvotes: 4

Emanuel
Emanuel

Reputation: 8106

If you really want to parse a very big json use a json library which allow streaming.

http://wiki.fasterxml.com/JacksonInFiveMinutes (check Streaming API Example)

Jackson does allow Streaming.

If you just dont know how to parse an array then you should change your question.

If you just want to parse an array you have to validate if there are { or [ which are either an object or an array. You can also verify if the result is an array or object by using

JSONObject json;
Object     info;
JSONArray  infoJsonArray;
JSONObject infoObject;
json = new JSONObject(str);

Object info= json.get("games");
if (info instanceof JSONArray) {
// It's an array
infoJsonArray = (JSONArray)info;
}
else if (info instanceof JSONObject) {
// It's an object
infoObject = (JSONObject)info;
} else {
// It's something else, like a string or number
}

Upvotes: 0

Related Questions