在json数组中解析不同数量的json对象android

时间:2015-06-09 09:59:59

标签: android json

我是来自服务器的这个json响应。

{
    "Patient Data 1": [
        {
            "Name": "priya",
            "Postal_Code": "110025",
            "Email_Id": "priysa@gmail.com"
        },
        {
            "Entered_Date": "8 Jul 2015",
            "EDD_Date": "15 Apr 2016"
        },
        {
            "Entered_Date": "06-08-2015",
            "Weight_log": "60"
        },
        {
            "Entered_Date": "27-09-2015",
            "Weight_log": "65"
        },
        {
            "Entered_Date": "28-09-2015",
            "Weight_log": "65"
        },
        {
            "Entered_Date": "8 Jul 2015",
            "Weight_log": "75"
        },
        {
            "Entered_Date": "8 Jul 2015",
            "Weight_log": "65"
        }
    ],
    "Patient Data 2": [
        {
            "Name": "priya",
            "Postal_Code": "110056",
            "Email_Id": "priya@yahoo.co.in"
        },
        {
            "Entered_Date": "8 Jul 2015",
            "EDD_Date": "15 Apr 2016"
        },
        {
            "Entered_Date": "8 Jun 2015",
            "Weight_log": "65"
        },
        {
            "Entered_Date": "8 Jun 2015",
            "Weight_log": "60"
        },
        {
            "Entered_Date": "8 Jul 2015",
            "Weight_log": "65"
        }
    ]
}

现在我想显示患者的列表视图,其中包含数据“Name”,“Postal_Code”,“Email Id”,“Entered_date”,“EDD_Date”和行项目中的按钮。因此,当点击此按钮时,我再次获得数据“Entered_Date”和“Weight_Date”的列表视图。

我已经解析了以下数据。

public void getPatientData(final String patientName){

    final ProgressDialog dialog = new ProgressDialog(this);
    dialog.setTitle("Please wait..");
    dialog.show();

    final String BASE_URL = "http://spirantcommunication.com/andriod/grl/doctor_show.php?";
    final String NAME_PARAM = "name";

    Uri uri = Uri.parse(BASE_URL).buildUpon()
            .appendQueryParameter(NAME_PARAM, patientName)
            .build();

    String url = uri.toString();

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null,

            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    dialog.dismiss();

                    int count = response.length();

                    try {
                        if (count == 1 && response.getJSONArray("Patient Data 1").
                                getJSONObject(0).getString("Message").equals("No Record Found for this Patient")){
                            Toast.makeText(getApplicationContext(), "No Record for this patient", Toast.LENGTH_SHORT).show();
                        } else if (count >= 1 && response.getJSONArray("Patient Data 1").
                                getJSONObject(0).getString("Name").equalsIgnoreCase(patientName)){

                            List<PatientModel> list = new ArrayList<>();

                            ArrayList<WeightModel> weightModels = new ArrayList<>();
                            PatientModel patientModel = null;

                            for (int i = 1; i <= count; i++){
                                try {
                                    JSONArray array = response.getJSONArray("Patient Data " + i);
                                    JSONObject jsonObject1 = array.getJSONObject(0);
                                    JSONObject jsonObject2 = array.getJSONObject(1);

                                    String patientName = jsonObject1.getString("Name");
                                    String patientPostalCode = jsonObject1.getString("Postal_Code");
                                    String patientEmailId = jsonObject1.getString("Email_Id");
                                    String patientEnteredEddDate = jsonObject2.getString("Entered_Date");
                                    String patientEddDate = jsonObject2.getString("EDD_Date");

                                    Log.v("Patient Name", patientName);
                                    Log.v("Patient Postal Code", patientPostalCode);
                                    Log.v("Patient Email Id", patientEmailId);
                                    Log.v("Selected Edd Date", patientEnteredEddDate);
                                    Log.v("Edd Date", patientEddDate);

                                    patientModel = new PatientModel();
                                    patientModel.setPatientName(patientName);
                                    patientModel.setPatientPostalCode(patientPostalCode);
                                    patientModel.setPatientEmail(patientEmailId);
                                    patientModel.setPatientSelectedEddDate(patientEnteredEddDate);
                                    patientModel.setPatientEddDate(patientEddDate);


                                    for (int j = 2; j < array.length(); j++){

                                        JSONObject object = array.getJSONObject(j);
                                        WeightModel weightModel = new WeightModel();
                                        weightModel.setSelectWeightDate(object.getString("Entered_Date"));
                                        weightModel.setWeightValue(object.getString("Weight_log"));
                                        weightModels.add(weightModel);
                                        patientModel.setWeightModels(weightModels);

                                        Log.v("Weight Entered Date", object.getString("Entered_Date"));
                                        Log.v("Weight Value", object.getString("Weight_log"));
                                    }
                                    list.add(patientModel);
                                }catch (JSONException e){
                                    e.printStackTrace();
                                }
                            }
                            PatientRecordCustomAdapter adapter = new PatientRecordCustomAdapter(list, getApplicationContext());
                            listView.setAdapter(adapter);
                        }
                    } catch (JSONException e){
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

    jsonObjectRequest.setTag(TAG);
    jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 1, 1.0f));
    ApplicationController.getInstance().addToRequestQueue(jsonObjectRequest, TAG);
}

我使用的模型类是

public class PatientModel {

    private String patientName;
    private String patientEmail;
    private String patientPostalCode;
    private String patientSelectedEddDate;
    private String patientEddDate;
    private ArrayList<WeightModel> weightModels = new ArrayList<>();

    public String getPatientName() {
        return patientName;
    }

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

    public String getPatientEmail() {
        return patientEmail;
    }

    public void setPatientEmail(String patientEmail) {
        this.patientEmail = patientEmail;
    }

    public String getPatientPostalCode() {
        return patientPostalCode;
    }

    public void setPatientPostalCode(String patientPostalCode) {
        this.patientPostalCode = patientPostalCode;
    }

    public String getPatientSelectedEddDate() {
        return patientSelectedEddDate;
    }

    public void setPatientSelectedEddDate(String patientSelectedEddDate) {
        this.patientSelectedEddDate = patientSelectedEddDate;
    }

    public String getPatientEddDate() {
        return patientEddDate;
    }

    public void setPatientEddDate(String patientEddDate) {
        this.patientEddDate = patientEddDate;
    }

    public ArrayList<WeightModel> getWeightModels() {
        return weightModels;
    }

    public void setWeightModels(ArrayList<WeightModel> weightModels) {
        this.weightModels = weightModels;
    }

}

我使用的自定义适配器是

公共类PatientRecordCustomAdapter扩展了BaseAdapter {

private List<PatientModel> data;
private Context context;

public PatientRecordCustomAdapter(List<PatientModel> data, Context context) {
    this.context = context;
    this.data = data;
}

@Override
public int getCount() {
    return data.size();
}

@Override
public Object getItem(int position) {
    return data.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    if (convertView == null){
        convertView = LayoutInflater.from(context).inflate(R.layout.item_patient_record, parent, false);
    }

    TextView patientName = (TextView) convertView.findViewById(R.id.tvPatientName);
    patientName.setText("Patient Name: " + data.get(position).getPatientName());

    TextView patientPostalCode = (TextView) convertView.findViewById(R.id.tvPostalCode);
    patientPostalCode.setText("Patient Postal Code: " + data.get(position).getPatientPostalCode());

    TextView patientEmailId = (TextView) convertView.findViewById(R.id.tvEmailId);
    patientEmailId.setText("Patient Email Id: " + data.get(position).getPatientEmail());

    TextView selectedEddDate = (TextView) convertView.findViewById(R.id.tvSelectedEddDate);
    selectedEddDate.setText("Patient Selected Edd Date: " + data.get(position).getPatientSelectedEddDate());

    TextView eddDate = (TextView) convertView.findViewById(R.id.tvEdd);
    eddDate.setText("Patient Edd Date: " + data.get(position).getPatientEddDate());

    ArrayList<WeightModel> list = data.get(position).getWeightModels();
    int size = list.size();

    Log.v("Size", String.valueOf(size));

    for (WeightModel model1 : list){
        Log.v("Selected Weight Date", model1.getSelectWeightDate());
        Log.v("Select Weight Value", model1.getWeightValue());
    }

    Button button = (Button) convertView.findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, CheckWeightDataActivity.class);
            // intent.putExtra("Weight List", (ArrayList<WeightModel>) weightData);
            //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //context.startActivity(intent);
        }
    });

    return convertView;
}

}

但是我将这个日志猫“Entered_Date”和“Weight_log”作为

06-09 15:26:13.398  24277-24277/com.spirant.grl V/Size﹕ 8
06-09 15:26:13.398  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 06-08-2015
06-09 15:26:13.398  24277-24277/com.spirant.grl V/Select Weight Value﹕ 60
06-09 15:26:13.398  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 27-09-2015
06-09 15:26:13.398  24277-24277/com.spirant.grl V/Select Weight Value﹕ 65
06-09 15:26:13.398  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 28-09-2015
06-09 15:26:13.398  24277-24277/com.spirant.grl V/Select Weight Value﹕ 65
06-09 15:26:13.398  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 8 Jul 2015
06-09 15:26:13.398  24277-24277/com.spirant.grl V/Select Weight Value﹕ 75
06-09 15:26:13.398  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 8 Jul 2015
06-09 15:26:13.398  24277-24277/com.spirant.grl V/Select Weight Value﹕ 65
06-09 15:26:13.398  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 8 Jun 2015
06-09 15:26:13.398  24277-24277/com.spirant.grl V/Select Weight Value﹕ 65
06-09 15:26:13.398  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 8 Jun 2015
06-09 15:26:13.399  24277-24277/com.spirant.grl V/Select Weight Value﹕ 60
06-09 15:26:13.399  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 8 Jul 2015
06-09 15:26:13.399  24277-24277/com.spirant.grl V/Select Weight Value﹕ 65
06-09 15:26:13.410  24277-24277/com.spirant.grl V/Size﹕ 8
06-09 15:26:13.410  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 06-08-2015
06-09 15:26:13.410  24277-24277/com.spirant.grl V/Select Weight Value﹕ 60
06-09 15:26:13.410  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 27-09-2015
06-09 15:26:13.410  24277-24277/com.spirant.grl V/Select Weight Value﹕ 65
06-09 15:26:13.410  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 28-09-2015
06-09 15:26:13.410  24277-24277/com.spirant.grl V/Select Weight Value﹕ 65
06-09 15:26:13.410  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 8 Jul 2015
06-09 15:26:13.410  24277-24277/com.spirant.grl V/Select Weight Value﹕ 75
06-09 15:26:13.410  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 8 Jul 2015
06-09 15:26:13.410  24277-24277/com.spirant.grl V/Select Weight Value﹕ 65
06-09 15:26:13.410  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 8 Jun 2015
06-09 15:26:13.410  24277-24277/com.spirant.grl V/Select Weight Value﹕ 65
06-09 15:26:13.410  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 8 Jun 2015
06-09 15:26:13.410  24277-24277/com.spirant.grl V/Select Weight Value﹕ 60
06-09 15:26:13.410  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 8 Jul 2015
06-09 15:26:13.410  24277-24277/com.spirant.grl V/Select Weight Value﹕ 65
06-09 15:26:13.415  24277-24277/com.spirant.grl V/Size﹕ 8
06-09 15:26:13.415  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 06-08-2015
06-09 15:26:13.415  24277-24277/com.spirant.grl V/Select Weight Value﹕ 60
06-09 15:26:13.415  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 27-09-2015
06-09 15:26:13.415  24277-24277/com.spirant.grl V/Select Weight Value﹕ 65
06-09 15:26:13.415  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 28-09-2015
06-09 15:26:13.415  24277-24277/com.spirant.grl V/Select Weight Value﹕ 65
06-09 15:26:13.415  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 8 Jul 2015
06-09 15:26:13.415  24277-24277/com.spirant.grl V/Select Weight Value﹕ 75
06-09 15:26:13.415  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 8 Jul 2015
06-09 15:26:13.415  24277-24277/com.spirant.grl V/Select Weight Value﹕ 65
06-09 15:26:13.415  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 8 Jun 2015
06-09 15:26:13.415  24277-24277/com.spirant.grl V/Select Weight Value﹕ 65
06-09 15:26:13.415  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 8 Jun 2015
06-09 15:26:13.415  24277-24277/com.spirant.grl V/Select Weight Value﹕ 60
06-09 15:26:13.415  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 8 Jul 2015
06-09 15:26:13.415  24277-24277/com.spirant.grl V/Select Weight Value﹕ 65
06-09 15:26:13.425  24277-24277/com.spirant.grl V/Size﹕ 8
06-09 15:26:13.425  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 06-08-2015
06-09 15:26:13.425  24277-24277/com.spirant.grl V/Select Weight Value﹕ 60
06-09 15:26:13.425  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 27-09-2015
06-09 15:26:13.425  24277-24277/com.spirant.grl V/Select Weight Value﹕ 65
06-09 15:26:13.425  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 28-09-2015
06-09 15:26:13.425  24277-24277/com.spirant.grl V/Select Weight Value﹕ 65
06-09 15:26:13.425  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 8 Jul 2015
06-09 15:26:13.425  24277-24277/com.spirant.grl V/Select Weight Value﹕ 75
06-09 15:26:13.425  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 8 Jul 2015
06-09 15:26:13.425  24277-24277/com.spirant.grl V/Select Weight Value﹕ 65
06-09 15:26:13.425  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 8 Jun 2015
06-09 15:26:13.425  24277-24277/com.spirant.grl V/Select Weight Value﹕ 65
06-09 15:26:13.425  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 8 Jun 2015
06-09 15:26:13.425  24277-24277/com.spirant.grl V/Select Weight Value﹕ 60
06-09 15:26:13.425  24277-24277/com.spirant.grl V/Selected Weight Date﹕ 8 Jul 2015
06-09 15:26:13.425  24277-24277/com.spirant.grl V/Select Weight Value﹕ 65

我得到的是四个而不是两个。请帮帮我。

2 个答案:

答案 0 :(得分:0)

首先根据您的要求更改您的json的结构。喜欢以下。

[
    {
        "Name": "priya",
        "Postal_Code": "110056",
        "Email_Id": "priya@yahoo.co.in",
        "weightModels": [
            {
                "Entered_Date": "8Jul2015",
                "EDD_Date": "15Apr2016"
            },
            {
                "Entered_Date": "8Jun2015",
                "Weight_log": "65"
            },
            {
                "Entered_Date": "8Jun2015",
                "Weight_log": "60"
            },
            {
                "Entered_Date": "8Jul2015",
                "Weight_log": "65"
            }
        ]
    },
    {
        "Name": "priya",
        "Postal_Code": "110056",
        "Email_Id": "priya@yahoo.co.in",
        "weightModels": [
            {
                "Entered_Date": "8Jul2015",
                "EDD_Date": "15Apr2016"
            },
            {
                "Entered_Date": "8Jun2015",
                "Weight_log": "65"
            },
            {
                "Entered_Date": "8Jun2015",
                "Weight_log": "60"
            },
            {
                "Entered_Date": "8Jul2015",
                "Weight_log": "65"
            }
        ]
    }
]

然后使用Gson

进行简单解析
Gson gson = new Gson();
for(int i=0;i<jsonArray.lenght();i++){
   PatientModel patient = gson.fromJson(jsonArray.get(i),PatientModel.class);
}

答案 1 :(得分:0)

如果你的预期结果加倍,那么你的循环可能就错了。

以loggin开头&#34; array.length()&#34;和&#34;计数&#34;值以查看它们是否具有预期值,如果大小超出预期值,则表示您已找到问题。

否则一步一步地调试你的代码(添加断点),你会更清楚地看到它出错的地方,然后盯着几页代码更容易