Skip to content Skip to sidebar Skip to footer

How To Parse Json Object With Array Retrofit

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',

Solution 1:

Create your POJO Model class from the json response

Passanger.java

publicclassPassanger {

@SerializedName("Passenger")
@Expose
private String passenger;
@SerializedName("BookingStatus")
@Expose
private String bookingStatus;
@SerializedName("CurrentStatus")
@Expose
private String currentStatus;

//implement getter,setter

}

TrainData.java

publicclassTrainData {

@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

publicinterfaceApi {

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

}

Make a request

Call<TrainData> call = getRetrofitInstance().create(Api.class).retriveTrainData();
            call.enqueue(newCallback<TrainData>() {
                @OverridepublicvoidonResponse(Call<TrainData> call, Response<TrainData> response) {
                    if(response.isSuccessful()) {
                         response.body().getPassangers();
                        //...
                    }
                }

                @OverridepublicvoidonFailure(Call<TrainData> call, Throwable t) {
                   //..
                }
            });

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

vogella

androidhive

android.jlelse

Solution 2:

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

Solution 3:

Here is the example :

Example.java

publicclassExample {

@SerializedName("PnrNumber")
@ExposeprivateString pnrNumber;
@SerializedName("Status")
@ExposeprivateString status;
@SerializedName("ResponseCode")
@ExposeprivateString responseCode;
@SerializedName("TrainNumber")
@ExposeprivateString trainNumber;
@SerializedName("TrainName")
@ExposeprivateString trainName;
@SerializedName("JourneyClass")
@ExposeprivateString journeyClass;
@SerializedName("ChatPrepared")
@ExposeprivateString chatPrepared;
@SerializedName("From")
@ExposeprivateStringfrom;
@SerializedName("To")
@ExposeprivateString to;
@SerializedName("JourneyDate")
@ExposeprivateString journeyDate;
@SerializedName("Passangers")
@ExposeprivateList<Passanger> passangers = null;

publicStringgetPnrNumber() {
return pnrNumber;
}

publicvoidsetPnrNumber(String pnrNumber) {
this.pnrNumber = pnrNumber;
}

publicStringgetStatus() {
return status;
}

publicvoidsetStatus(String status) {
this.status = status;
}

publicStringgetResponseCode() {
return responseCode;
}

publicvoidsetResponseCode(String responseCode) {
this.responseCode = responseCode;
}

publicStringgetTrainNumber() {
return trainNumber;
}

publicvoidsetTrainNumber(String trainNumber) {
this.trainNumber = trainNumber;
}

publicStringgetTrainName() {
return trainName;
}

publicvoidsetTrainName(String trainName) {
this.trainName = trainName;
}

publicStringgetJourneyClass() {
return journeyClass;
}

publicvoidsetJourneyClass(String journeyClass) {
this.journeyClass = journeyClass;
}

publicStringgetChatPrepared() {
return chatPrepared;
}

publicvoidsetChatPrepared(String chatPrepared) {
this.chatPrepared = chatPrepared;
}

publicStringgetFrom() {
returnfrom;
}

publicvoidsetFrom(Stringfrom) {
this.from = from;
}

publicStringgetTo() {
return to;
}

publicvoidsetTo(String to) {
this.to = to;
}

publicStringgetJourneyDate() {
return journeyDate;
}

publicvoidsetJourneyDate(String journeyDate) {
this.journeyDate = journeyDate;
}

publicList<Passanger> getPassangers() {
return passangers;
}

publicvoidsetPassangers(List<Passanger> passangers) {
this.passangers = passangers;
}

}

Passanger.Java

publicclassPassanger {

@SerializedName("Passenger")
@ExposeprivateString passenger;
@SerializedName("BookingStatus")
@ExposeprivateString bookingStatus;
@SerializedName("CurrentStatus")
@ExposeprivateString currentStatus;

publicStringgetPassenger() {
return passenger;
}

publicvoidsetPassenger(String passenger) {
this.passenger = passenger;
}

publicStringgetBookingStatus() {
return bookingStatus;
}

publicvoidsetBookingStatus(String bookingStatus) {
this.bookingStatus = bookingStatus;
}

publicStringgetCurrentStatus() {
return currentStatus;
}

publicvoidsetCurrentStatus(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;


GsonBuildergsonBuilder=newGsonBuilder();
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

Post a Comment for "How To Parse Json Object With Array Retrofit"