简体   繁体   English

如何使用 retrofit 解析数组内部的 object

[英]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 Json: https://api.github.com/users

This is URL of an API... how can i parse this object to fetch the data using retrofit?这是 API 的 URL...我如何解析这个 object 以使用 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.下面是有关如何使用 retrofit 获取数组 JSON object 的示例。我相信您不会遇到更改它以使用您的数据的麻烦。

Example.java例子.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乘客.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 响应生成 POJO class。 JSON TO POJO JSON 到 POJO

Add this gradle:添加此 gradle:

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

Init the Gson buildr:初始化 Gson 构建器:

private Gson gson;


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

Parse the JSON using GSON使用 GSON 解析 JSON

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

These are the basic steps to parse the JSON using GSON.这些是使用 GSON 解析 JSON 的基本步骤。

For more information you can refer to the below article:有关更多信息,您可以参考以下文章:

Parsing JSON on Android using GSON 使用 GSON 在 Android 上解析 JSON

Or Check the GSON official GitHub Repository或者检查 GSON 官方 GitHub 存储库

GSON GSON

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用翻新Android解析对象内部的数组 - How to Parse Array inside Object With Retrofit Android 我如何使用翻新版2.3.0解析json对象 - How can i parse json object using retrofit 2.3.0 如何使用Retrofit和GSON解析JSON数组? - How can I parse JSON Array Using Retrofit and GSON? 如何使用改造解析json数组? - How to parse json array using retrofit? 如何使用Retrofit从JSON对象获取密钥? - How can i get Key from JSON Object using Retrofit? 当回复有时是一个对象而有时是一个数组时,如何在使用改进时解析JSON回复? - How to parse a JSON reply when using retrofit when the reply is sometimes an object and sometimes an array? 如何解析一个JSON,其中对象有时充当Retrofit中的字符串或数组 - How to parse a json in which the object sometime acts as a string or an array in Retrofit 如何使用Gson解析深层嵌套json对象中的字段并在android中进行改造? - How do I parse a field from a deep nested json object using Gson and retrofit in android? I want to pass json object as a raw data using @Body in retrofit, but I am unable to add an image file inside the Request Object, how can I achieve it - I want to pass json object as a raw data using @Body in retrofit, but I am unable to add an image file inside the Request Object, how can I achieve it 如何在改造中解析 json 数组 - How to parse json array in retrofit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM