gpt4 book ai didi

kotlin - 在Kotlin中渲染json响应

转载 作者:行者123 更新时间:2023-12-02 13:24:09 24 4
gpt4 key购买 nike

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

如果您能给我看一个可以解决这个问题的示例类,将不胜感激。

最佳答案

该示例类代码为您提供了 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来使用您的类名称。

关于kotlin - 在Kotlin中渲染json响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46740775/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com