如何在gson库+ android + java中使用json响应

时间:2014-10-23 03:58:45

标签: java android gson

我正在接受网络服务回复。我在这里得到回复。

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

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

现在我需要使用gson库我制作所有模型或点和持有者类

持有人:

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;
}
}

响应

   {
              "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
            }

如何使用gson在 holder 对象中获取所有Web服务对象

1 个答案:

答案 0 :(得分:0)

您可以尝试使用GSON中的fromJSON()

这是一个例子

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