2015-08-28 54 views
0

我正在使用完全像這樣返回數據的端點。對POJO的改進響應

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

我試圖用改造請求數據,並將其解析爲這樣一個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; 
    } 
} 

改造回拉正確的數據。我可以在我的日誌中看到它。 但是,它似乎無法將其解析爲我的「Parada」模型。

這是我的要求:

@GET("/Parada/Buscar?termosBusca=*") 
public void getAllStops(Callback<List<Parada>> cb); 

我敢肯定它的小東西。只是不能發現它。

UPDATE

包括我restAdapter。

RestAdapter restAdapter = new RestAdapter.Builder() 
      .setLogLevel(RestAdapter.LogLevel.FULL) 
      .setEndpoint(myEndpoint) 
      .setConverter(new GsonConverter(new GsonBuilder().create())).build(); 

回答

4

使用Gson註解@SerializedName()將屬性名稱映射到java成員。

e.g:

@SerializedName("Latitude") 
String latitude; 

@SerializedName("Longitude") 
String longitude; 
+0

這工作。謝謝。我沒有意識到它是區分大小寫的。對其他人來說,我必須在所有領域都做到這一點。 –

2

我知道你打上以前的答案是正確的。但爲了防止別人讀這個。您可以使用此頁面將任何JSON轉換爲合適的POJO。 http://www.jsonschema2pojo.org/它的偉大工程

0

使用此鏈接創建性反應在改造API來POJO

http://www.jsonschema2pojo.org/ 

enter image description here

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

}