1

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;
        }
3
  • 1
    A SSCC-example would be great, as we neither know what response is, nor why you even have that for-loop in place. Commented Dec 24, 2011 at 20:55
  • Found the solution, see below. Thanks anyway :) Commented Dec 24, 2011 at 23:15
  • No problem, glad you did it by yourself! Commented Dec 25, 2011 at 16:44

2 Answers 2

4

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();
        }
Sign up to request clarification or add additional context in comments.

Comments

1
//         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

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.