PvdL
PvdL

Reputation: 1593

Looping and converting JSON Object to Array

I'm new to Java and programming for Android and I've seen a lot of tutorials but I am kinda clueless atm on how to loop through a JSONObject and set it to my class.

Example of JSON data: http://sickbeard.com/api/#history

Class I made:

public Episode(JSONObject obj) {
        try {
            this.id =   Integer.parseInt(obj.getString("episode").toString());
            this.tvId = Integer.parseInt(obj.getString("tvdbid").toString());
            this.resource = obj.getString("resource").toString();
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

I came as far as this...

ArrayList<Episode> episodeList = new ArrayList<Episode>();
            JSONObject data = new JSONObject();
            for(int i = 0; i < 2; i++) {
                try {
                    data = response.getJSONObject("data");
                    episodeList.add(new Episode(data));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            return null;
            // for each entry create new episode :)
        } else {
            return null;
        }

Upvotes: 1

Views: 31285

Answers (2)

Irakli dd
Irakli dd

Reputation: 56

//         is = entity.getContent();
ArrayList<String> myList = new ArrayList<String>();


              //convert response to string
            try{

                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                result = sb.toString();


            } catch (Exception e) {
                Log.e("log_tag", "Error converting result "+e.toString());
            }




            //parse json data
            try{
                    jArray = new JSONArray(result);


                    for(int i=0;i<jArray.length();i++){

                        json_data = jArray.getJSONObject(i);

                        myList.add(json_data.getString("id"));
                        Log.i("log_tag","id: " + json_data.getString("id"));
                    }

            }
            catch(JSONException e){
                    Log.e("log_tag", "Error parsing data "+e.toString());
            }


return myList;

// then u can recieve this myList :

ArrayList<String> get_data_id = postData();
// get_data_id = myList
get_data_id.get(0) - it is first element,
get_data_id.get(1) - it is second element

.... EXAMPLE

json data is : [{"id":"1"},{"id":"2"},{"id":"3"},{"id":"4"},{"id":"5"},{"id":"6"}]
                        {in loop}   myList.add(json_data.getString("id"));
get_data_id.get(0) = 1
get_data_id.get(1) = 2
get_data_id.get(2) = 3
..........
:)

Good Luck

Upvotes: 1

PvdL
PvdL

Reputation: 1593

Found it :)

try {
            response = new JSONObject(con.query("history", parameters));
            JSONArray data = response.getJSONArray("data");
            for(int i = 0; i < data.length(); i++) {
                try {
                    episodeList.add(new Episode((JSONObject) data.get(i)));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

Upvotes: 4

Related Questions