如何从数组retrofit2中的数组中获取数据

时间:2016-08-10 09:49:20

标签: android json multidimensional-array retrofit2

请参考此参考

想要预约arraylist来扩充listview,我到达call_detail但是如何通过retrofit2 gsonConverter获得预约数组

{
"patient":{},
"medicine":[
],
"call_detail":[
{
"created_on":"2016-01-22 06:06:00",
"call_type":"",
"rbs":"10",
"temp":"",
"provisional_diagnosis":"yes",
"follow_up_call_schedule":"",
"follow_up_call_date":"1970-01-01",
"reports":[
],
"medicalreference":[
],
"services_comment":"",
"health_consultation":"",
"appointment":[
{
"doctor_id":"28",
"clinic":"",
"hospital":"Shwe Nyaung Pin specialist ",
"name":"Dr.Nyein Mon Yu",
"app_date":"2016-01-21",
"app_time":"1:00 AM TO 1:00 AM"
}
]
}
],
"status":"true"
}

2 个答案:

答案 0 :(得分:0)

试试这个..

    try {
        JSONObject jsonObject=new JSONObject(response);

        JSONArray jsonArray=jsonObject.getJSONArray("call_detail");

        for(int i=0;i<jsonArray.length();i++)
        {
            JSONObject jsonObjectCall=jsonArray.getJSONObject(i);

            JSONArray jsonArrayAppointment=jsonObjectCall.getJSONArray("appointment");

            for (int j=0;j<jsonArrayAppointment.length();j++)
            {
                JSONObject jsonObjectApp=jsonArrayAppointment.getJSONObject(j);

                String doctor_id=jsonObjectApp.getString("doctor_id");
                String clinic=jsonObjectApp.getString("clinic");
                String hospital=jsonObjectApp.getString("hospital");
                String name=jsonObjectApp.getString("name");
                String app_date=jsonObjectApp.getString("app_date");
                String app_time=jsonObjectApp.getString("app_time");

                System.out.println("doctor_id"+doctor_id);

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

答案 1 :(得分:0)

要获得预约数组,请尝试以下代码。这里jsonString是你问的json:

    try {
            JSONObject jsonObject=new JSONObject(jsonString);
            JSONArray appointmentArray=jsonObject.getJSONArray("call_detail").getJSONObject(0).getJSONArray("appointment");
            Log.d("appointmentArray",appointmentArray.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }