How to parse JSON object with array retrofit [duplicate]

孤街浪徒 提交于 2021-01-05 09:47:47

问题


how to fetch JSON object with array using android retrofit. I am using retrofit as web API.

My JSON Response is as follows:-

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

回答1:


Create your POJO Model class from the json response

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 interface

public interface Api {

    @GET("/your_api_endpoint")
    Call<TrainData> retriveTrainData();

}

Make a request

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) {
                   //..
                }
            });

There are plenty of resources online, for more details you can read the following articles

vogella

androidhive

android.jlelse




回答2:


Create two classes. One for main json object and other one for passenger. Put a ArrayList of Passengers in first class




回答3:


Here is the example :

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

}

Here are the classes which are generated from the Response which you have provided in the question. You can use this link to generate the POJO class for JSON response. JSON TO POJO

Add this gradle:

implementation 'com.google.code.gson:gson:2.8.2'

Init the Gson buildr:

private Gson gson;


GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setDateFormat("M/d/yy hh:mm a");
gson = gsonBuilder.create();

Parse the JSON using GSON

gson.fromJson(jsonObject.getJSONObject("data").toString(), Example.class);

These are the basic steps to parse the JSON using GSON.

For more information you can refer to the below article:

Parsing JSON on Android using GSON

Or Check the GSON official GitHub Repository

GSON



来源:https://stackoverflow.com/questions/58162960/how-to-parse-json-object-with-array-retrofit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!