-4

JSON Response:

{"schedulePlan":[{"slot_id":1,"slot":"08:00 AM - 09:00 AM","slotStatus":"booked","booking_details":[{"booking_id":"33","patientName":"Malik","patientAddress":"D-77, Sector 63, Noida"}]},{"slot_id":2,"slot":"09:00 AM - 10:00 AM","slotStatus":"available","booking_details":[{"booking_id":"","patientName":"","patientAddress":""}]},{"slot_id":3,"slot":"10:00 AM - 11:00 AM","slotStatus":"booked","booking_details":[{"booking_id":"22","patientName":"Malik","patientAddress":"D-77, Sector 63, Noida"},{"booking_id":55,"patientName":"Om","patientAddress":"Sector 33, Noida"}]},{"slot_id":4,"slot":"11:00 AM - 12:00 PM","slotStatus":"available","booking_details":[{"booking_id":"","patientName":"","patientAddress":""}]},{"slot_id":5,"slot":"12:00 PM - 01:00 PM","slotStatus":"booked","booking_details":[{"booking_id":"76","patientName":"Raj","patientAddress":"Sector 83, Noida"}]}]}

Output Required:

Patient Name: Malik
Patient Name:
Patient Name: Malik, Om
Patient Name:
Patient Name: Raj

And same for address as well.

On every list item click, need to display patient name. How to parse the response and display patientName and patientAddress as above?

Models: Model 1:

public class RescheduleModel {
private String slot_id;
private String slot;
private String slotStatus;
public ArrayList<BookingDetailsModel> bookingDetailsModel = new ArrayList<BookingDetailsModel>();

public String getSlot_id() {
    return slot_id;
}

public void setSlot_id(String slot_id) {
    this.slot_id = slot_id;
}

public String getSlot() {
    return slot;
}

public void setSlot(String availableSlot) {
    this.slot = availableSlot;
}

public String getSlotStatus() {
    return slotStatus;
}

public void setSlotStatus(String slotStatus) {
    this.slotStatus = slotStatus;
}

public List<BookingDetailsModel> getBookingDetailsModels() {
    return bookingDetailsModel;
}

public void setBookingDetailsModel(ArrayList<BookingDetailsModel> bookingDetailsModel) {
    this.bookingDetailsModel = bookingDetailsModel;
}

Model 2:

public class BookingDetailsModel {
private String booking_id;
private String patientName;
private String patientAddress;

public String getBooking_id() {
    return booking_id;
}

public void setBooking_id(String booking_id) {
    this.booking_id = booking_id;
}

public String getPatientName() {
    return patientName;
}

public void setPatientName(String patientName) {
    this.patientName = patientName;
}

public String getPatientAddress() {
    return patientAddress;
}

public void setPatientAddress(String patientAddress) {
    this.patientAddress = patientAddress;
}

Activity:

public void parseJsonResponse(String result) {
    Log.i(TAG, result);
    try {
        JSONObject json = new JSONObject(result);
        JSONArray jArray = new JSONArray(json.getString("schedulePlan"));
        for (int i = 0; i < jArray.length(); i++) {

            JSONObject jObject = jArray.getJSONObject(i);

            RescheduleModel rescheduleModel = new RescheduleModel();

            rescheduleModel.setSlot_id(jObject.getString("slot_id"));
            rescheduleModel.setSlot(jObject.getString("slot"));
            rescheduleModel.setSlotStatus(jObject.getString("slotStatus"));

            JSONArray array = jObject.getJSONArray("booking_details");
            for (int j = 0; j < array.length(); j++) {
                JSONObject object = array.getJSONObject(j);

                BookingDetailsModel model = new BookingDetailsModel();

                model.setPatientName(object.getString("patientName"));
                model.setPatientAddress(object.getString("patientAddress"));

                Log.i("PNAME...", object.getString("patientName"));
                Log.i("PADDRESS...", object.getString("patientAddress"));

                bookingDetails.add(model);
            }
            slots.add(rescheduleModel);
        }

        adapter.notifyDataSetChanged();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

Output:

04-21 15:17:04.431 28588-28588/com.example I/PNAME...: Malik
04-21 15:17:04.431 28588-28588/com.example I/PADDRESS...: D-77, Sector 63, 
Noida
04-21 15:17:04.431 28588-28588/com.example I/PNAME...: Malik
04-21 15:17:04.431 28588-28588/com.example I/PADDRESS...: D-77, Sector 63, 
Noida
04-21 15:17:04.431 28588-28588/com.example I/PNAME...: Om
04-21 15:17:04.431 28588-28588/com.example I/PADDRESS...: Sector 33, Noida
04-21 15:17:04.431 28588-28588/com.example I/PNAME...: Raj
04-21 15:17:04.432 28588-28588/com.example I/PADDRESS...: Sector 83, Noida

I want to display "Malik, Om" when i click third row in the listview. Currently Malik and Om are getting added to different rows.

OnClick:

nextDetail.setText("Next slot is booked for '" + bookingDetails.get(position + 1).getPatientName() + "' at '" +
                            bookingDetails.get(position + 1).getPatientAddress() + "'.");
ABC
  • 3
  • 3

7 Answers7

1

Here is the working code:

public void parseJsonResponse(String result) {

    Log.i("JSON", result);

    try {
        JSONObject json = new JSONObject(result);
        JSONArray jArray = new JSONArray(json.getString("schedulePlan"));
        for (int i = 0; i < jArray.length(); i++) {

            JSONObject jObject = jArray.getJSONObject(i);

            JSONArray array = jObject.getJSONArray("booking_details");

            StringBuilder stringBuilderName = new StringBuilder();
            StringBuilder stringBuilderAddress = new StringBuilder();

            for (int j = 0; j < array.length(); j++) {
                JSONObject object = array.getJSONObject(j);

                String name = object.getString("patientName");
                String address = object.getString("patientAddress");

                if (TextUtils.isEmpty(name))
                    stringBuilderName.append(" ");
                else
                    stringBuilderName.append(name);

                if (TextUtils.isEmpty(address))
                    stringBuilderAddress.append(" ");
                else
                    stringBuilderAddress.append(address);

                if (j < array.length() - 1) {
                    stringBuilderName.append(", ");
                    stringBuilderAddress.append(", ");
                }
            }

            Log.i("PNAME...", stringBuilderName.toString());
            Log.i("PADDRESS...", stringBuilderAddress.toString());
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
}

OUTPUT:

04-21 16:34:37.842 8171-8171/com.ferdous.myapplication5 I/JSON: {"schedulePlan":[{"slot_id":1,"slot":"08:00 AM - 09:00 AM","slotStatus":"booked","booking_details":[{"booking_id":"33","patientName":"Malik","patientAddress":"D-77, Sector 63, Noida"}]},{"slot_id":2,"slot":"09:00 AM - 10:00 AM","slotStatus":"available","booking_details":[{"booking_id":"","patientName":"","patientAddress":""}]},{"slot_id":3,"slot":"10:00 AM - 11:00 AM","slotStatus":"booked","booking_details":[{"booking_id":"22","patientName":"Malik","patientAddress":"D-77, Sector 63, Noida"},{"booking_id":55,"patientName":"Om","patientAddress":"Sector 33, Noida"}]},{"slot_id":4,"slot":"11:00 AM - 12:00 PM","slotStatus":"available","booking_details":[{"booking_id":"","patientName":"","patientAddress":""}]},{"slot_id":5,"slot":"12:00 PM - 01:00 PM","slotStatus":"booked","booking_details":[{"booking_id":"76","patientName":"Raj","patientAddress":"Sector 83, Noida"}]}]}
04-21 16:34:37.842 8171-8171/com.ferdous.myapplication5 I/PNAME...: Malik
04-21 16:34:37.842 8171-8171/com.ferdous.myapplication5 I/PADDRESS...: D-77, Sector 63, Noida
04-21 16:34:37.852 8171-8171/com.ferdous.myapplication5 I/PNAME...:  
04-21 16:34:37.852 8171-8171/com.ferdous.myapplication5 I/PADDRESS...:  
04-21 16:34:37.852 8171-8171/com.ferdous.myapplication5 I/PNAME...: Malik, Om
04-21 16:34:37.852 8171-8171/com.ferdous.myapplication5 I/PADDRESS...: D-77, Sector 63, Noida, Sector 33, Noida
04-21 16:34:37.852 8171-8171/com.ferdous.myapplication5 I/PNAME...:  
04-21 16:34:37.852 8171-8171/com.ferdous.myapplication5 I/PADDRESS...:  
04-21 16:34:37.852 8171-8171/com.ferdous.myapplication5 I/PNAME...: Raj
04-21 16:34:37.852 8171-8171/com.ferdous.myapplication5 I/PADDRESS...: Sector 83, Noida
Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61
  • Hi, You answer helped a lot but i'm facing one issue. I saved the strings to model and when i'm trying to get data from that model, first two rows show correct data but when i click third, it shows only Malik and on fourth row on wards it shows correct data. But this third row data is extra. How to get rid of this? Added to model like this: BookingDetailsModel model = new BookingDetailsModel(); model.setPatientName(stringBuilderName.toString()); model.setPatientAddress(stringBuilderAddress.toString()); – ABC Apr 21 '17 at 11:12
  • And on item click : textview.setText(bookingDetails.get(position).getPatientName() + bookingDetails.get(position).getPatientAddress()); – ABC Apr 21 '17 at 11:14
  • where you added below codes: BookingDetailsModel model = new BookingDetailsModel(); model.setPatientName(stringBuilderName.toString()); model.setPatientAddress(stringBuilderAddress.toString()); is it inside for loop or outside of for loop – Ferdous Ahamed Apr 21 '17 at 11:21
  • I added it inside the loop. Corrected the same and its working now. Thanks. I really appreciate your help. – ABC Apr 21 '17 at 11:26
  • My pleasure. I am really happy to help you :) – Ferdous Ahamed Apr 21 '17 at 11:28
  • Can i ask you one last thing related to this problem only? When i click the last row, i get an error: java.lang.IndexOutOfBoundsException: Invalid index 5, size is 5 How can i resolve this? – ABC Apr 21 '17 at 12:08
  • Try this when you load tube data: for(int i=0; i – Ferdous Ahamed Apr 21 '17 at 12:10
  • Didn't worked. If possible kindly share the updated code. – ABC Apr 21 '17 at 12:33
  • Its better if you can create a differnt question with your current code – Ferdous Ahamed Apr 21 '17 at 12:36
  • I'm unable to post a new question. So please help me here only. Code is exactly the same as you sent. I'm just trying to get the data of next row on click. Till 4th row its absolutely fine but when i click the last row i.e. 5th, it throws an error "java.lang.IndexOutOfBoundsException: Invalid index 5, size is 5". How can i get rid of this? – ABC Apr 21 '17 at 12:58
  • Can you please post code that you used to show patient name from list by list item click. Don't remove existing code from this question. just add the code. – Ferdous Ahamed Apr 21 '17 at 13:43
  • Use bookingDetails.get(position) instead of bookingDetails.get(position + 1) – Ferdous Ahamed Apr 21 '17 at 14:19
  • I did this because i also want next row's data. bookingDetails.get(position) is working fine and even bookingDetails.get(position+1) also, but the issue is only when i click the last item/row. – ABC Apr 21 '17 at 15:28
  • In your for loop condition use: position < list.size() - 1 – Ferdous Ahamed Apr 21 '17 at 15:38
0

You can do like below code

  //jsonObject is your jsonData which you get From Server 
    try {
        JSONArray array = jsonObject.getJSONArray("schedulePlan");
        for (int i = 0; i < array.length(); i++) {
            JSONObject data = array.getJSONObject(i);
            int SlotId = data.getInt("slot_id");
            String slot = data.getString("slot");
            String slotStatus = data.getString("slotStatus");

            JSONArray booking_details = data.getJSONArray("booking_details");
            for (int j = 0; j < booking_details.length(); j++) {
                JSONObject bookingData = booking_details.getJSONObject(i);
                String booking_id = bookingData.getString("booking_id");
                String patientName = bookingData.getString("patientName");
                String patientAddress = bookingData.getString("patientAddress");
            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

Like this you can parse and Make POJO class or store variable and can use where ever you want

Nikhil Sharma
  • 593
  • 7
  • 23
0
  try {
            JSONObject jsonObject = new JSONObject(result);
            JSONArray jsonArray = null;

            jsonArray = jsonObject.getJSONArray("schedulePlan");

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                JSONObject jsonObject2 = jsonObject1.getJSONArray("booking_details").getJSONObject(0);
                jsonObject2.getString("patient_name");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
Shubham
  • 2,627
  • 3
  • 20
  • 36
0
Here is the code

 String json = Your Json

    JSONObject root = new JSONObject  (json);
    JSONArray array = root.JSONArray("schedulePlan") ;
    for( int i =0;i<array.length;i++){
    JSONObject obj= array .JSONObject  (i);
    JSONArray booking_details= obj.JSONArray("booking_details") ;
    String patientName= booking_details.getString("patientName");
    String patientAddress= booking_details.getString("patientAddress");

    }
Harsh Singhal
  • 567
  • 4
  • 12
0

I think you are using volley then try this out:

class Patient{
private String name,address
Patient(String name,String address)
{
  this.name=name;
  this.address=address;
}
}

class SampleClass{ 
//create a arrayList of patient
ArrayList<String> patientArraryList= new ArrayList<>();
/*
  your code
*/


//in your on response section use this code
    public void onResponse(JSONObject response) 
{
  JSONArray jarray=response.getJSONArray("schedulePlan");

 for(int i=0;i<jarray.length();i++){
    JSONObject temp= jarray.getJSONObject(i);
    JSONArray jarray2=temp.getJSONArray("booking_details");
      for(int j=0;j<=jarray.length();j++){
       patientArraryList.add(
       new Patient(
       jarray2.getJSONObject(j).getString("patientName"),
       jarray2.getJSONObject(j).getString("patientAddress")));
     }

  }

}
//now your data is ready and is in patientArraryList
//use adapter to show in list, you can find many examples of //adapter 
0

put your JSON string here and select below options in the url:

Source type:JSON

Annotation style:Gson

it will create model classes using json string and add all created classes in your app

then add this gradle compile 'com.google.code.gson:gson:2.7' in your build gradle

and try this Example example = new Gson().fromJson("Your Json String",Example.class);

Bhupat Bheda
  • 1,968
  • 1
  • 8
  • 13
-1

If you check in your json data privided by you, for third item you have two booking details, so when you click on recyclerview 3rd item, first you need to add those two parentnames into one string by using for loop then set that resulted data to textView.

Hope this helpful..

@Rajesh

Rajesh Peram
  • 1,128
  • 8
  • 10