在Kotlin中渲染json响应

时间:2017-10-14 04:09:12

标签: kotlin

我是Kotlin和Java的新手。 我尝试进行API调用,然后在Kotlin中进行渲染。

这就是我获得JSON响应的方式:

[
  {
    "AWBNo": "1326217373504",
    "AuthKey": "Valid",
    "OrderNo": "SGHRGR15073TCC",
    "ReturnMessage": "Successful",
    "ShipmentSummary": [
      {
        "PickUpDate": "08-10-2017",
        "PickUpTime": "0015",
        "OriginLocation": "DEL/WDL, Delhi NCR, DELHI",
        "DestinationLocation": "",
        "Weight": "0",
        "ExpectedDeliveryDate": "10/11/2017 12:21:32 AM",
        "Status": "Delivered",
        "StatusCode": "DLVD",
        "StatusDate": "11-10-2017",
        "StatusTime": "1316",
        "Location": "Berhampur, Berhampur, ORISSA",
        "Comment": "Shipment Delivered by SR: RAKESH, DeliveryDate:2017-10-11 1316, Receiver Name: Manmandir  Mobile Accessories  Remarks : "
      },
      {
        "PickUpDate": "08-10-2017",
        "PickUpTime": "0015",
        "OriginLocation": "DEL/WDL, Delhi NCR, DELHI",
        "DestinationLocation": "",
        "Weight": "0",
        "ExpectedDeliveryDate": "10/11/2017 12:21:32 AM",
        "Status": "Out for Delivery",
        "StatusCode": "OFD",
        "StatusDate": "11-10-2017",
        "StatusTime": "0858",
        "Location": "Berhampur, Berhampur, ORISSA",
        "Comment": "Out for delivery: 39031-RAKESH-PDS1728408582139031"
      }
    ]
  }
]

如何在java对象中使用它。

我知道我们需要创建一个类似于我们收到的响应的POJO数据类。

如果你能告诉我一个可以使用它的样本课,

将不胜感激。

1 个答案:

答案 0 :(得分:2)

此示例类代码为您提供 GSON 库注释类代码。您可以使用它来解析您的json。

如果要在Kotlin和java中使用普通类,可以简单地删除注释。

您可以使用以下Kotlin类来解析您的json响应:

Kotlin类代码:

链接:https://pastebin.com/gXq3TrXf

 class Example {
  @SerializedName("AWBNo")
  @Expose
  var awbNo:String
  @SerializedName("AuthKey")
  @Expose
  var authKey:String
  @SerializedName("OrderNo")
  @Expose
  var orderNo:String
  @SerializedName("ReturnMessage")
  @Expose
  var returnMessage:String
  @SerializedName("ShipmentSummary")
  @Expose
  var shipmentSummary:List<ShipmentSummary> = null
  inner class ShipmentSummary {
    @SerializedName("PickUpDate")
    @Expose
    var pickUpDate:String
    @SerializedName("PickUpTime")
    @Expose
    var pickUpTime:String
    @SerializedName("OriginLocation")
    @Expose
    var originLocation:String
    @SerializedName("DestinationLocation")
    @Expose
    var destinationLocation:String
    @SerializedName("Weight")
    @Expose
    var weight:String
    @SerializedName("ExpectedDeliveryDate")
    @Expose
    var expectedDeliveryDate:String
    @SerializedName("Status")
    @Expose
    var status:String
    @SerializedName("StatusCode")
    @Expose
    var statusCode:String
    @SerializedName("StatusDate")
    @Expose
    var statusDate:String
    @SerializedName("StatusTime")
    @Expose
    var statusTime:String
    @SerializedName("Location")
    @Expose
    var location:String
    @SerializedName("Comment")
    @Expose
    var comment:String
  }
}

Java类代码:

链接:https://pastebin.com/zrPNB0T4

public class Example {

@SerializedName("AWBNo")
@Expose
private String aWBNo;
@SerializedName("AuthKey")
@Expose
private String authKey;
@SerializedName("OrderNo")
@Expose
private String orderNo;
@SerializedName("ReturnMessage")
@Expose
private String returnMessage;
@SerializedName("ShipmentSummary")
@Expose
private List<ShipmentSummary> shipmentSummary = null;

public String getAWBNo() {
return aWBNo;
}

public void setAWBNo(String aWBNo) {
this.aWBNo = aWBNo;
}

public String getAuthKey() {
return authKey;
}

public void setAuthKey(String authKey) {
this.authKey = authKey;
}

public String getOrderNo() {
return orderNo;
}

public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}

public String getReturnMessage() {
return returnMessage;
}

public void setReturnMessage(String returnMessage) {
this.returnMessage = returnMessage;
}

public List<ShipmentSummary> getShipmentSummary() {
return shipmentSummary;
}

public void setShipmentSummary(List<ShipmentSummary> shipmentSummary) {
this.shipmentSummary = shipmentSummary;
}


public class ShipmentSummary {

@SerializedName("PickUpDate")
@Expose
private String pickUpDate;
@SerializedName("PickUpTime")
@Expose
private String pickUpTime;
@SerializedName("OriginLocation")
@Expose
private String originLocation;
@SerializedName("DestinationLocation")
@Expose
private String destinationLocation;
@SerializedName("Weight")
@Expose
private String weight;
@SerializedName("ExpectedDeliveryDate")
@Expose
private String expectedDeliveryDate;
@SerializedName("Status")
@Expose
private String status;
@SerializedName("StatusCode")
@Expose
private String statusCode;
@SerializedName("StatusDate")
@Expose
private String statusDate;
@SerializedName("StatusTime")
@Expose
private String statusTime;
@SerializedName("Location")
@Expose
private String location;
@SerializedName("Comment")
@Expose
private String comment;

public String getPickUpDate() {
return pickUpDate;
}

public void setPickUpDate(String pickUpDate) {
this.pickUpDate = pickUpDate;
}

public String getPickUpTime() {
return pickUpTime;
}

public void setPickUpTime(String pickUpTime) {
this.pickUpTime = pickUpTime;
}

public String getOriginLocation() {
return originLocation;
}

public void setOriginLocation(String originLocation) {
this.originLocation = originLocation;
}

public String getDestinationLocation() {
return destinationLocation;
}

public void setDestinationLocation(String destinationLocation) {
this.destinationLocation = destinationLocation;
}

public String getWeight() {
return weight;
}

public void setWeight(String weight) {
this.weight = weight;
}

public String getExpectedDeliveryDate() {
return expectedDeliveryDate;
}

public void setExpectedDeliveryDate(String expectedDeliveryDate) {
this.expectedDeliveryDate = expectedDeliveryDate;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public String getStatusCode() {
return statusCode;
}

public void setStatusCode(String statusCode) {
this.statusCode = statusCode;
}

public String getStatusDate() {
return statusDate;
}

public void setStatusDate(String statusDate) {
this.statusDate = statusDate;
}

public String getStatusTime() {
return statusTime;
}

public void setStatusTime(String statusTime) {
this.statusTime = statusTime;
}

public String getLocation() {
return location;
}

public void setLocation(String location) {
this.location = location;
}

public String getComment() {
return comment;
}

public void setComment(String comment) {
this.comment = comment;
}

}

}

您可以通过替换Example来使用您的班级名称。