如何使用android改造来获取带有数组的JSON对象。我正在使用翻新作为Web API。
我的JSON响应如下:-
{
"PnrNumber": "12345665",
"Status": "SUCCESS",
"ResponseCode": "200",
"TrainNumber": "1111",
"TrainName": "ABC",
"JourneyClass": "SL",
"ChatPrepared": "NO",
"From": "TEST1",
"To": "TEST2",
"JourneyDate": "2019-10-01",
"Passangers": [
{
"Passenger": "Passenger 1",
"BookingStatus": "CNF/S4/18",
"CurrentStatus": "CNF/S4/18"
},
{
"Passenger": "Passenger 2",
"BookingStatus": "CNF/S4/19",
"CurrentStatus": "CNF/S4/19"
},
{
"Passenger": "Passenger 3",
"BookingStatus": "CNF/S4/24",
"CurrentStatus": "CNF/S4/24"
}
]
}
答案 0 :(得分:4)
根据json响应创建POJO模型类
Passanger.java
public class Passanger {
@SerializedName("Passenger")
@Expose
private String passenger;
@SerializedName("BookingStatus")
@Expose
private String bookingStatus;
@SerializedName("CurrentStatus")
@Expose
private String currentStatus;
//implement getter,setter
}
TrainData.java
public class TrainData {
@SerializedName("PnrNumber")
@Expose
private String pnrNumber;
@SerializedName("Status")
@Expose
private String status;
@SerializedName("ResponseCode")
@Expose
private String responseCode;
@SerializedName("TrainNumber")
@Expose
private String trainNumber;
@SerializedName("TrainName")
@Expose
private String trainName;
@SerializedName("JourneyClass")
@Expose
private String journeyClass;
@SerializedName("ChatPrepared")
@Expose
private String chatPrepared;
@SerializedName("From")
@Expose
private String from;
@SerializedName("To")
@Expose
private String to;
@SerializedName("JourneyDate")
@Expose
private String journeyDate;
@SerializedName("Passangers")
@Expose
private List<Passanger> passangers = null;
//implement getter, setter
}
Retrofit api界面
public interface Api {
@GET("/your_api_endpoint")
Call<TrainData> retriveTrainData();
}
提出请求
Call<TrainData> call = getRetrofitInstance().create(Api.class).retriveTrainData();
call.enqueue(new Callback<TrainData>() {
@Override
public void onResponse(Call<TrainData> call, Response<TrainData> response) {
if(response.isSuccessful()) {
response.body().getPassangers();
//...
}
}
@Override
public void onFailure(Call<TrainData> call, Throwable t) {
//..
}
});
在线资源很多,有关更多详细信息,您可以阅读以下文章
答案 1 :(得分:1)
创建两个类。一个用于主json对象,另一个用于乘客。在头等舱乘客中放置一个ArrayList
答案 2 :(得分:1)
这里是示例:
Example.java
public class Example {
@SerializedName("PnrNumber")
@Expose
private String pnrNumber;
@SerializedName("Status")
@Expose
private String status;
@SerializedName("ResponseCode")
@Expose
private String responseCode;
@SerializedName("TrainNumber")
@Expose
private String trainNumber;
@SerializedName("TrainName")
@Expose
private String trainName;
@SerializedName("JourneyClass")
@Expose
private String journeyClass;
@SerializedName("ChatPrepared")
@Expose
private String chatPrepared;
@SerializedName("From")
@Expose
private String from;
@SerializedName("To")
@Expose
private String to;
@SerializedName("JourneyDate")
@Expose
private String journeyDate;
@SerializedName("Passangers")
@Expose
private List<Passanger> passangers = null;
public String getPnrNumber() {
return pnrNumber;
}
public void setPnrNumber(String pnrNumber) {
this.pnrNumber = pnrNumber;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getResponseCode() {
return responseCode;
}
public void setResponseCode(String responseCode) {
this.responseCode = responseCode;
}
public String getTrainNumber() {
return trainNumber;
}
public void setTrainNumber(String trainNumber) {
this.trainNumber = trainNumber;
}
public String getTrainName() {
return trainName;
}
public void setTrainName(String trainName) {
this.trainName = trainName;
}
public String getJourneyClass() {
return journeyClass;
}
public void setJourneyClass(String journeyClass) {
this.journeyClass = journeyClass;
}
public String getChatPrepared() {
return chatPrepared;
}
public void setChatPrepared(String chatPrepared) {
this.chatPrepared = chatPrepared;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getJourneyDate() {
return journeyDate;
}
public void setJourneyDate(String journeyDate) {
this.journeyDate = journeyDate;
}
public List<Passanger> getPassangers() {
return passangers;
}
public void setPassangers(List<Passanger> passangers) {
this.passangers = passangers;
}
}
Passanger.Java
public class Passanger {
@SerializedName("Passenger")
@Expose
private String passenger;
@SerializedName("BookingStatus")
@Expose
private String bookingStatus;
@SerializedName("CurrentStatus")
@Expose
private String currentStatus;
public String getPassenger() {
return passenger;
}
public void setPassenger(String passenger) {
this.passenger = passenger;
}
public String getBookingStatus() {
return bookingStatus;
}
public void setBookingStatus(String bookingStatus) {
this.bookingStatus = bookingStatus;
}
public String getCurrentStatus() {
return currentStatus;
}
public void setCurrentStatus(String currentStatus) {
this.currentStatus = currentStatus;
}
}
这是从问题中提供的响应中生成的类。 您可以使用此链接为JSON响应生成POJO类。 JSON TO POJO
添加此gradle:
implementation 'com.google.code.gson:gson:2.8.2'
初始化Gson构建器:
private Gson gson;
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setDateFormat("M/d/yy hh:mm a");
gson = gsonBuilder.create();
使用GSON解析JSON
gson.fromJson(jsonObject.getJSONObject("data").toString(), Example.class);
这些是使用GSON解析JSON的基本步骤。
有关更多信息,您可以参考以下文章:
Parsing JSON on Android using GSON
或查看GSON官方GitHub存储库