简体   繁体   中英

How to use json response in gson library + android + java

I am getting web service response this.I am getting response here .

@Override
    public void getWebserviceResponse(String result) {
        // TODO Auto-generated method stub

    Log.d("-----", "naveen"+ result);
    }

Now I need to use gson library I make all model or dot and holder class

holder:

public class Holder {
    List<deparaturedaseboarddto> data;
}

**DTO:**

package com.firstgroup.dto;

public class deparaturedaseboarddto {
    @SerializedName("alertsId")
 int alertsId;
    @SerializedName("destExpArrival")
 String destExpArrival;
    @SerializedName("destSchArrival")
 String destSchArrival;
    @SerializedName("expDepart")
 String expDepart;
    @SerializedName("filteredStation")
 String filteredStation;
    @SerializedName("platformNo")
 String platformNo;
    @SerializedName("rid")
 String rid;
    @SerializedName("schDepart")
 String schDepart;
    @SerializedName("toc")
 String toc;
    @SerializedName("toExpArrival")
 String toExpArrival;
    @SerializedName("toSchArrival")
 String toSchArrival;
    @SerializedName("trainID")
 String trainID;
    @SerializedName("trainLastReportedAt")
 String trainLastReportedAt;
    @SerializedName("destinationStation")
 DestinationStation destinationStation;
    public deparaturedaseboarddto(String trainID,String toc,String trainLastReportedAt, String platformNo, String schDepart, String expDepart, int alertsId, String rid, String destSchArrival, String filteredStation, String destExpArrival, String toSchArrival, String toExpArrival,DestinationStation destinationStation){
        this.trainID=trainID;
        this.toc=toc;
        this.trainLastReportedAt=trainLastReportedAt;
        this.platformNo=platformNo;
        this.schDepart=schDepart;
        this.expDepart=expDepart;
        this.alertsId=alertsId;
        this.destinationStation=destinationStation;
        this.toExpArrival=toExpArrival;
        this.toSchArrival=toSchArrival;
        this.destExpArrival=destExpArrival;
        this.filteredStation=filteredStation;
        this.destSchArrival=destSchArrival;
        this.rid=rid;

    }
public DestinationStation getDestinationStation() {
    return destinationStation;
}
public void setDestinationStation(DestinationStation destinationStation) {
    this.destinationStation = destinationStation;
}
public int getAlertsId() {
    return alertsId;
}
public void setAlertsId(int alertsId) {
    this.alertsId = alertsId;
}
public String getDestExpArrival() {
    return destExpArrival;
}
public void setDestExpArrival(String destExpArrival) {
    this.destExpArrival = destExpArrival;
}
public String getDestSchArrival() {
    return destSchArrival;
}
public void setDestSchArrival(String destSchArrival) {
    this.destSchArrival = destSchArrival;
}
public String getExpDepart() {
    return expDepart;
}
public void setExpDepart(String expDepart) {
    this.expDepart = expDepart;
}
public String getFilteredStation() {
    return filteredStation;
}
public void setFilteredStation(String filteredStation) {
    this.filteredStation = filteredStation;
}
public String getPlatformNo() {
    return platformNo;
}
public void setPlatformNo(String platformNo) {
    this.platformNo = platformNo;
}
public String getRid() {
    return rid;
}
public void setRid(String rid) {
    this.rid = rid;
}
public String getSchDepart() {
    return schDepart;
}
public void setSchDepart(String schDepart) {
    this.schDepart = schDepart;
}
public String getToc() {
    return toc;
}
public void setToc(String toc) {
    this.toc = toc;
}
public String getToExpArrival() {
    return toExpArrival;
}
public void setToExpArrival(String toExpArrival) {
    this.toExpArrival = toExpArrival;
}
public String getToSchArrival() {
    return toSchArrival;
}
public void setToSchArrival(String toSchArrival) {
    this.toSchArrival = toSchArrival;
}
public String getTrainID() {
    return trainID;
}
public void setTrainID(String trainID) {
    this.trainID = trainID;
}
public String getTrainLastReportedAt() {
    return trainLastReportedAt;
}
public void setTrainLastReportedAt(String trainLastReportedAt) {
    this.trainLastReportedAt = trainLastReportedAt;
}
}

Response

   {
              "data": [


                {
                  "trainID": "2A04",
                  "toc": "SE",
                  "trainLastReportedAt": null,
                  "platformNo": "2  ",
                  "destinationStation": {
                    "crsCode": "VIC",
                    "stationName": "London Victoria"
                  },
                  "schDepart": "06:43",
                  "expDepart": "06:43",
                  "alertsId": 0,
                  "rid": "201410231556369",
                  "destSchArrival": "06:53",
                  "destExpArrival": "06:53",
                  "filteredStation": null,
                  "toSchArrival": null,
                  "toExpArrival": null
                },
                {
                  "trainID": "2O00",
                  "toc": "TL",
                  "trainLastReportedAt": null,
                  "platformNo": "1  ",
                  "destinationStation": {
                    "crsCode": "LUT",
                    "stationName": "Luton"
                  },
                  "schDepart": "06:46",
                  "expDepart": "06:46",
                  "alertsId": 0,
                  "rid": "201410231543833",
                  "destSchArrival": "07:53",
                  "destExpArrival": "07:53",
                  "filteredStation": null,
                  "toSchArrival": null,
                  "toExpArrival": null
                }
              ],
              "alertDetailPopulated": false,
              "success": true,
              "alertData": null
            }

how to get all objects of web service in holder object using gson

You can try to use fromJSON() from GSON

this is an example

@Override
public void getWebserviceResponse(String result) {
    Log.d("-----", "naveen"+ result);
    Holder data = new Gson().fromJson(result, Holder.class);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM