Aditiya raj
Aditiya raj

Reputation: 1

I want to parse this json in Android but I'm getting repeated data

JSON:

{
   "videos":[
      [
         {
            "video_id":"DKOLynNhWxo",
            "video_url":"https:\/\/www.youtube.com\/watch?v=DKOLynNhWxo",
            "video_host":"youtube",
            "created_at":"2017-08-08 11:17:00",
            "branch_name":"Computer Science",
            "semester_name":"Semester 1",
            "subject_name":"English"
         },
         {
            "video_id":"haYm5k6h5yc",
            "video_url":"https:\/\/www.youtube.com\/watch?v=haYm5k6h5yc",
            "video_host":"Youtube",
            "created_at":"2017-08-10 10:05:00",
            "branch_name":"Computer Science",
            "semester_name":"Semester 1",
            "subject_name":"English"
         }
      ],
      [
         {
            "video_id":"VSkRU8eXFII",
            "video_url":"https:\/\/www.youtube.com\/watch?v=VSkRU8eXFII",
            "video_host":"youtube",
            "created_at":"2017-08-08 11:18:00",
            "branch_name":"Computer Science",
            "semester_name":"Semester 1",
            "subject_name":"Maths"
         }
      ],
      [],
      []
   ]
}

Code:

private class GetContacts extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(JsonExp.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();


        }

        @Override
        protected Void doInBackground(Void... arg0) {
            AppController sh = new AppController();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url);

            Log.e(TAG, "Response from url: " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);

                    // Getting JSON Array node
                    JSONArray contacts = jsonObj.getJSONArray("videos");

                    // looping through All Contacts
                    for (int i = 0; i < contacts.length(); i++) {

                        //JSONObject cjsnobj = contacts.getJSONObject(i);
                        JSONArray c = contacts.getJSONArray(i);
                        for (int y = 0; y < c.length(); y++)
                        {
                            JSONObject obj=c.getJSONObject(i);
                            String aid = obj.getString("video_id");
                            String url = obj.getString("video_url");
                           // String name = obj.getString("video_host");
                            // tmp hash map for single contact
                            HashMap<String, String> contact = new HashMap<>();

                            // adding each child node to HashMap key => value
                            contact.put("video_id", aid);
                            contact.put("video_url", url);
                           // contact.put("video_host", name);


                            // adding contact to contact list
                            contactList.add(contact);
                        }
                    }
                } catch (final JSONException e) {
                    Log.e(TAG, "Json parsing error: " + e.getMessage());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),
                                    "Json parsing error: " + e.getMessage(),
                                    Toast.LENGTH_LONG)
                                    .show();
                        }
                    });

                }
            } else {
                Log.e(TAG, "Couldn't get json from server.");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Couldn't get json from server. Check LogCat for possible errors!",
                                Toast.LENGTH_LONG)
                                .show();
                    }
                });

            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();
            /**
             * Updating parsed JSON data into ListView
             * */
            ListAdapter adapter = new SimpleAdapter(
                    JsonExp.this, contactList,
                    R.layout.list_item, new String[]{"video_id","video_url"//, "video_host"//, "ProductDetails", "rectimestamp"
            }, new int[]{R.id.aid, R.id.name
                  });

            lv.setAdapter(adapter);
        }

    }

Upvotes: 0

Views: 72

Answers (2)

Fatih Santalu
Fatih Santalu

Reputation: 4701

replace JSONObject obj=c.getJSONObject(i); with JSONObject obj=c.getJSONObject(y);

I strongly suggest you to use Gson

The example:

Create a model class

public class Video {

    private String id;
    private String url;
    private String host;
    private String date;
    private String branch;
    private String semester;
    private String subject;

    public Video(String id, String url, String host, String date, String branch, String semester, String subject) {
        this.id = id;
        this.url = url;
        this.host = host;
        this.date = date;
        this.branch = branch;
        this.semester = semester;
        this.subject = subject;
    }

    public String getId() {
        return id;
    }

    public String getUrl() {
        return url;
    }

    public String getHost() {
        return host;
    }

    public String getDate() {
        return date;
    }

    public String getBranch() {
        return branch;
    }

    public String getSemester() {
        return semester;
    }

    public String getSubject() {
        return subject;
    }
}

Create an arraylist

    JSONArray contacts = jsonObj.getJSONArray("videos");
    ArrayList<Video> videos = new ArrayList<>();
    for (int i = 0; i < contacts.length(); i++) {
        JSONObject contact = contacts.getJSONObject(i);
        for (int y = 0; y < contact.length(); y++) {
            JSONObject obj = contact.getJSONObject(y);
            String id = obj.getString("video_id");
            String url = obj.getString("video_url");
            String host = obj.getString("video_host");
            String date = obj.getString("created_at");
            String branch = obj.getString("branch_name");
            String semester = obj.getString("semester_name");
            String subject = obj.getString("subject_name");

            Video video = new Video(id, url, host, date, branch, semester, subject);
            videos.add(video);
        }
    }

Upvotes: 3

Mehul Kabaria
Mehul Kabaria

Reputation: 6622

try{
        JSONObject obj = nes JSONObject(yourResponse);
        JSONArray video = obj.getJSONArray("videos");

  for(i=0; i<video.length; i++){
      JSONObject object  = video.getJSONObject(i); 
     String subject_name = object.getString("subject_name");
   }
}
catch(JSONException e){
}

Upvotes: 0

Related Questions