在Kotlin中渲染json响应

我是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数据类。

将不胜感激,如果你能告诉我一个样本class将与此合作。

这个示例类代码为您提供了一个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 = 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 = 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 getShipmentSummary() { return shipmentSummary; } public void setShipmentSummary(List 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来使用您的类名称。