数据覆盖前一个

时间:2016-12-19 04:43:54

标签: android json

这是我的api

[  
   {  
      "id":1,
      "name":"Little Angels Higher Secondary School",
      "address":"Hattiban",
      "district":"Lalitpur",
      "country":"Nepal",
      "phone":"01-41111111",
      "email":"lac@gmail.com",
      "website":"wwww.la.com.np",
      "institution_type":"Government",
      "establishment_date":"15-12-2016",
      "admission_open_from":"15-12-2016",
      "admission_open_to":"19-12-2016",
      "logo":"http:\/\/www.myeducationhunt.com\/logos\/logo1.png",
      "active":0,
      "created_at":null,
      "updated_at":null,
      "latitude":27.6514,
      "longitude":85.3359,
      "fees":[  
         {  
            "id":1,
            "grade":"1",
            "price":12000,
            "school_id":1,
            "created_at":null,
            "updated_at":null
         },
         {  
            "id":2,
            "grade":"2",
            "price":13000,
            "school_id":1,
            "created_at":null,
            "updated_at":null
         }
      ]
   },
   {  
      "id":6,
      "name":"Baltimore Secondary School",
      "address":"Baltimore",
      "district":"idk",
      "country":"U.S",
      "phone":"1111111111111",
      "email":"bat@gmail.com",
      "website":"www.baltimore.edu.np",
      "institution_type":"Private",
      "establishment_date":"15-12-2016",
      "admission_open_from":"15-12-2016",
      "admission_open_to":"20-12-2016",
      "logo":"http:\/\/www.myeducationhunt.com\/logos\/logo2.png",
      "active":0,
      "created_at":null,
      "updated_at":null,
      "latitude":27.6514,
      "longitude":85.3359,
      "fees":[  
         {  
            "id":9,
            "grade":"1",
            "price":12000,
            "school_id":6,
            "created_at":null,
            "updated_at":null
         },
         {  
            "id":10,
            "grade":"2",
            "price":12500,
            "school_id":6,
            "created_at":null,
            "updated_at":null
         },
         {  
            "id":11,
            "grade":"3",
            "price":15000,
            "school_id":6,
            "created_at":null,
            "updated_at":null
         },
         {  
            "id":12,
            "grade":"4",
            "price":19000,
            "school_id":6,
            "created_at":null,
            "updated_at":null
         }
      ]
   }]

我在学校课程中将这些数据解析为:

JsonArrayRequest schoolRequest = new JsonArrayRequest(url,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());
                        hidePDialog();

                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {
                            try {

                                JSONObject obj = response.getJSONObject(i);
                                OurSchool ourSchool = new OurSchool();

                                ourSchool.schoolId = obj.getInt("id");
                                ourSchool.schoolName = obj.getString("name");
                                ourSchool.schoolLocation = obj.getString("address");
                                ourSchool.schoolLogo = obj.getString("logo");
                                ourSchool.district=obj.getString("district");
                                ourSchool.country=obj.getString("country");
                                ourSchool.phone=obj.getString("phone");
                                ourSchool.email=obj.getString("email");
                                ourSchool.website=obj.getString("website");
                                ourSchool.type=obj.getString("institution_type");
                                ourSchool.estb_date=obj.getString("establishment_date");
                                ourSchool.admission_start_date=obj.getString("admission_open_from");
                                ourSchool.admission_end_date=obj.getString("admission_open_to");
                                ourSchool.latitude=obj.getDouble("latitude");
                                ourSchool.longitude=obj.getDouble("longitude");
                                // adding schools to ourSchool list

                                JSONArray fees=obj.getJSONArray("fees");
                                List<FeeClass> listFeeClass= new ArrayList<FeeClass>();//by Ahamed
                                for (int j=0;j<fees.length(); j++){
                                    FeeClass feeClass= new FeeClass();//by Ahamed
                                    JSONObject obj1=fees.getJSONObject(j);
                                       feeClass.setGrade(obj1.getString("grade"));//By Ahamed
                                    feeClass.setFee(obj1.getString("price"));//ByAhamed

                                   listFeeClass.add(feeClass);//by Ahamed

                                    ourSchool.grade=obj1.getString("grade");//you need to remove this line
                                    ourSchool.fee=obj1.getString("price");//you need to remove this line
                                }
                                ourSchool.setFeesList(listFeeClass);//by Ahamed
                                ourSchoolsListItems.add(ourSchool);

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

                        adapter.notifyDataSetChanged();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                hidePDialog();
            }
        });

我将这些数据发送到我的schooldetail课程

Intent i = new Intent(School.this, SchoolDetails.class);

                i.putExtra("school", ourSchoolsListItems.get(position));
                startActivity(i);

最后我尝试将相应学校的所有这些数据检索为

 ourSchool = (OurSchool) getActivity().getIntent().getSerializableExtra("school");

一切都很好。唯一的问题是内部&#34;费用&#34; api中的对象。我想显示相应课程的所有费用细节。但我只是收到最后一个对象的费用细节&#34;费用&#34;。例如在小天使的学费范围内,我只是获得2级的成绩,等级和价格。我想检索费用数组中的所有对象详细信息。请帮助。

更新 //由艾哈迈德

 class FeeClass{
       String grade,fee;
    public void setGrade(String grade){
      this.grade=grade;
    }
    public String getGrade(){
      return this.grade;
    }
 }

在OurSchool课程中添加以下变量。

    List<FeeClass> feeClassObject= new ArrayList<FeeClass>();

还有getter和setter

    public void setFeesList(List<FeeClass> feeClassObject){
      this.feeClassObject=feeClassObject;
    }
    public List<FeeClass> getFeesList(){
      return  feeClassObject;
    }

并查看您的for循环代码

3 个答案:

答案 0 :(得分:1)

因为,你的第一个循环学校创建了一个单独的OurSchool对象。在单个的ourchool对象中,您试图将所有费用详细信息放在单个指针对象中。因此,第一个费用详细信息保存在该费用详细信息对象上,并再次收取第二个费用详细信同样的事情,直到你的第二个循环完成。所以,最后一项只存在于该对象上。就这样。快乐的编码。

修改

因此,您必须在ourSchool对象上维护一个List。

像,

class FeeObject{
 String feeDetails1,feeDetails2..etc
}

在OurSchoolObject中添加以下变量。

  List<FeeObject> feeObjectList= new ArrayList<>();

这就是全部

答案 1 :(得分:0)

使用此模型类来解析json。

public class OurSchool {
public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getDistrict() {
    return district;
}

public void setDistrict(String district) {
    this.district = district;
}

public String getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country = country;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getWebsite() {
    return website;
}

public void setWebsite(String website) {
    this.website = website;
}

public String getInstitutionType() {
    return institutionType;
}

public void setInstitutionType(String institutionType) {
    this.institutionType = institutionType;
}

public String getEstablishmentDate() {
    return establishmentDate;
}

public void setEstablishmentDate(String establishmentDate) {
    this.establishmentDate = establishmentDate;
}

public String getAdmissionOpenFrom() {
    return admissionOpenFrom;
}

public void setAdmissionOpenFrom(String admissionOpenFrom) {
    this.admissionOpenFrom = admissionOpenFrom;
}

public String getAdmissionOpenTo() {
    return admissionOpenTo;
}

public void setAdmissionOpenTo(String admissionOpenTo) {
    this.admissionOpenTo = admissionOpenTo;
}

public String getLogo() {
    return logo;
}

public void setLogo(String logo) {
    this.logo = logo;
}

public int getActive() {
    return active;
}

public void setActive(int active) {
    this.active = active;
}

public String getCreatedAt() {
    return createdAt;
}

public void setCreatedAt(String createdAt) {
    this.createdAt = createdAt;
}

public String getUpdatedAt() {
    return updatedAt;
}

public void setUpdatedAt(String updatedAt) {
    this.updatedAt = updatedAt;
}

public double getLatitude() {
    return latitude;
}

public void setLatitude(double latitude) {
    this.latitude = latitude;
}

public double getLongitude() {
    return longitude;
}

public void setLongitude(double longitude) {
    this.longitude = longitude;
}

public List<Fees> getFees() {
    return fees;
}

public void setFees(List<Fees> fees) {
    this.fees = fees;
}

@SerializedName("id")
public int id;
@SerializedName("name")
public String name;
@SerializedName("address")
public String address;
@SerializedName("district")
public String district;
@SerializedName("country")
public String country;
@SerializedName("phone")
public String phone;
@SerializedName("email")
public String email;
@SerializedName("website")
public String website;
@SerializedName("institution_type")
public String institutionType;
@SerializedName("establishment_date")
public String establishmentDate;
@SerializedName("admission_open_from")
public String admissionOpenFrom;
@SerializedName("admission_open_to")
public String admissionOpenTo;
@SerializedName("logo")
public String logo;
@SerializedName("active")
public int active;
@SerializedName("created_at")
public String createdAt;
@SerializedName("updated_at")
public String updatedAt;
@SerializedName("latitude")
public double latitude;
@SerializedName("longitude")
public double longitude;
@SerializedName("fees")
public List<Fees> fees;

public static class Fees {
    @SerializedName("id")
    public int id;
    @SerializedName("grade")
    public String grade;
    @SerializedName("price")
    public int price;
    @SerializedName("school_id")
    public int schoolId;
    @SerializedName("created_at")
    public String createdAt;
    @SerializedName("updated_at")
    public String updatedAt;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getSchoolId() {
        return schoolId;
    }

    public void setSchoolId(int schoolId) {
        this.schoolId = schoolId;
    }

    public String getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(String createdAt) {
        this.createdAt = createdAt;
    }

    public String getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(String updatedAt) {
        this.updatedAt = updatedAt;
    }
}
  }

然后添加依赖项

compile 'com.google.code.gson:gson:2.8.0'

答案 2 :(得分:0)

请在下面这样做。如果有任何混淆,请告诉我。

public class OurSchool {
private ArrayList<Fees> feeList;

public void setFees(ArrayList<Fees> list){
this.feeList=list
}

publice ArrayList<Fees> getFeeList(){
return feeList;
}

}


public class Fees{
private long id;
private long school_id;
private int grade;
private int fee;
}

然后OnResonse方法就是这样的

JsonArrayRequest schoolRequest = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {

                -------other code -------


                JSONArray fees=obj.getJSONArray("fees");


                          ArrayList<Fees> feeList=new ArrayList<Fees>;
                            for (int j=0;j<fees.length(); j++){
                                final Fees fees=new Fees();
                                JSONObject obj1=fees.getJSONObject(j);
                                fees.grade=obj1.getString("grade");
                                fees.fee=obj1.getString("price");
                                feeList.add(fees)
                            }
                            ourSchool.setFees(feeList); /// Add the feeList ///

                            ourSchoolsListItems.add(ourSchool);// Add the OurSchool ///



                }