简体   繁体   中英

How can I parse object inside array using retrofit

[
{
"login": "mojombo",
"id": 1,
"node_id": "MDQ6VXNlcjE=",
"avatar_url": "https://avatars0.githubusercontent.com/u/1?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/mojombo",
"html_url": "https://github.com/mojombo",
"followers_url": "https://api.github.com/users/mojombo/followers",
"following_url": "https://api.github.com/users/mojombo/following{/other_user}",
"gists_url": "https://api.github.com/users/mojombo/gists{/gist_id}",
"starred_url": "https://api.github.com/users/mojombo/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/mojombo/subscriptions",
"organizations_url": "https://api.github.com/users/mojombo/orgs",
"repos_url": "https://api.github.com/users/mojombo/repos",
"events_url": "https://api.github.com/users/mojombo/events{/privacy}",
"received_events_url": "https://api.github.com/users/mojombo/received_events",
"type": "User",
"site_admin": false
}
]

Json: https://api.github.com/users

This is URL of an API... how can i parse this object to fetch the data using retrofit?

Here is the example on how to fetch JSON object with array using retrofit. I believe you won't have troubles changing it to work with your data.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM