我正在使用一个返回数据的端点。
[
{
"CodigoParada":4201929,
"Nome":"",
"Endereco":"RUA JOAO DE LIMA BONFANTE (B-C)",
"Latitude":-23.687475,
"Longitude":-46.771474
},
{
"CodigoParada":640000453,
"Nome":"ACANGAPIRANGA C/B",
"Endereco":" R CONSTANTINO NERY/ AV BENEDITO ANDRADE ",
"Latitude":-23.485653,
"Longitude":-46.71567
}
]
我正在尝试使用Retrofit来请求数据并将其解析为这样的POJO:
public class Parada {
private int codigoParada;
private String nome;
private String endereco;
private String latitude;
private String longitude;
public int getCodigoParada() {
return codigoParada;
}
public void setCodigoParada(int codigoParada) {
this.codigoParada = codigoParada;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getEndereco() {
return endereco;
}
public void setEndereco(String endereco) {
this.endereco = endereco;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
}
Retrofit正确地拉回数据。我可以在我的日志中看到它。 但是,似乎无法将其解析为我的“Parada”模型。
这是我的要求:
@GET("/Parada/Buscar?termosBusca=*")
public void getAllStops(Callback<List<Parada>> cb);
我确定它的东西很小。只是无法发现它。
更新
包括我的restAdapter。
RestAdapter restAdapter = new RestAdapter.Builder()
.setLogLevel(RestAdapter.LogLevel.FULL)
.setEndpoint(myEndpoint)
.setConverter(new GsonConverter(new GsonBuilder().create())).build();
答案 0 :(得分:4)
使用Gson注释@SerializedName()将属性名称映射到java成员。
e.g:
@SerializedName("Latitude")
String latitude;
@SerializedName("Longitude")
String longitude;
答案 1 :(得分:2)
我知道您将上一个答案标记为正确。但以防其他人阅读此内容。您可以使用此页面将任何JSON转换为适当的POJO。 http://www.jsonschema2pojo.org/效果很好
答案 2 :(得分:0)
使用此链接在Retrofit Api中创建对pojo的响应
http://www.jsonschema2pojo.org/
-----------------------------------com.example.YourClassName.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class YourClassName {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("isImportant")
@Expose
private Boolean isImportant;
@SerializedName("picture")
@Expose
private String picture;
@SerializedName("from")
@Expose
private String from;
@SerializedName("subject")
@Expose
private String subject;
@SerializedName("message")
@Expose
private String message;
@SerializedName("timestamp")
@Expose
private String timestamp;
@SerializedName("isRead")
@Expose
private Boolean isRead;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Boolean getIsImportant() {
return isImportant;
}
public void setIsImportant(Boolean isImportant) {
this.isImportant = isImportant;
}
public String getPicture() {
return picture;
}
public void setPicture(String picture) {
this.picture = picture;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public Boolean getIsRead() {
return isRead;
}
public void setIsRead(Boolean isRead) {
this.isRead = isRead;
}
}