我的改装 json class 应该如何?
How should my retrofit json class be?
我有这样的json
{
"sentences": [
{
"trans": "It works.",
"orig": "Это работает.",
"translit": "",
"src_translit": "Eto rabotayet."
}
],
"src": "ru",
"server_time": 12
}
代码:
public interface TraslatorRetrofit {
@GET("/translate_a/t?client=x&text=text&hl=ru&sl=ru&tl=en")
void listRepos (@Query("text") String text, Callback<List<TranslateResp>> cb);}
public class TranslateResp {
private List<String> sentences;
}
但我总是收到这样的错误"Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column"
我的 class 应该是什么样子?谢谢
如果请求响应是这样的
{"sentences":
[
{
"trans":"It works.","orig":"Это работает.",
"translit":"","src_translit":"Eto rabotayet.",
"src":"ru",
"server_time":12
}
]
}
那么,您的 类 将是
public class Sentence {
@Expose
private String trans;
@Expose
private String orig;
@Expose
private String translit;
@SerializedName("src_translit")
@Expose
private String srcTranslit;
@Expose
private String src;
@SerializedName("server_time")
@Expose
private Integer serverTime;
public String getTrans() {
return trans;
}
public void setTrans(String trans) {
this.trans = trans;
}
public String getOrig() {
return orig;
}
public void setOrig(String orig) {
this.orig = orig;
}
public String getTranslit() {
return translit;
}
public void setTranslit(String translit) {
this.translit = translit;
}
public String getSrcTranslit() {
return srcTranslit;
}
public void setSrcTranslit(String srcTranslit) {
this.srcTranslit = srcTranslit;
}
public String getSrc() {
return src;
}
public void setSrc(String src) {
this.src = src;
}
public Integer getServerTime() {
return serverTime;
}
public void setServerTime(Integer serverTime) {
this.serverTime = serverTime;
}
}
主要回应Class
public class TranslateResp {
@Expose
private List<Sentence> sentences = new ArrayList<Sentence>();
public List<Sentence> getSentences() {
return sentences;
}
public void setSentences(List<Sentence> sentences) {
this.sentences = sentences;
}
}
这是 JSON GEN 的另一个好东西 - 客户端代码生成工具 http://jsongen.byingtondesign.com/
我有这样的json
{ "sentences": [ { "trans": "It works.", "orig": "Это работает.", "translit": "", "src_translit": "Eto rabotayet." } ], "src": "ru", "server_time": 12 }
代码:
public interface TraslatorRetrofit {
@GET("/translate_a/t?client=x&text=text&hl=ru&sl=ru&tl=en")
void listRepos (@Query("text") String text, Callback<List<TranslateResp>> cb);}
public class TranslateResp {
private List<String> sentences;
}
但我总是收到这样的错误"Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column"
我的 class 应该是什么样子?谢谢
如果请求响应是这样的
{"sentences":
[
{
"trans":"It works.","orig":"Это работает.",
"translit":"","src_translit":"Eto rabotayet.",
"src":"ru",
"server_time":12
}
]
}
那么,您的 类 将是
public class Sentence {
@Expose
private String trans;
@Expose
private String orig;
@Expose
private String translit;
@SerializedName("src_translit")
@Expose
private String srcTranslit;
@Expose
private String src;
@SerializedName("server_time")
@Expose
private Integer serverTime;
public String getTrans() {
return trans;
}
public void setTrans(String trans) {
this.trans = trans;
}
public String getOrig() {
return orig;
}
public void setOrig(String orig) {
this.orig = orig;
}
public String getTranslit() {
return translit;
}
public void setTranslit(String translit) {
this.translit = translit;
}
public String getSrcTranslit() {
return srcTranslit;
}
public void setSrcTranslit(String srcTranslit) {
this.srcTranslit = srcTranslit;
}
public String getSrc() {
return src;
}
public void setSrc(String src) {
this.src = src;
}
public Integer getServerTime() {
return serverTime;
}
public void setServerTime(Integer serverTime) {
this.serverTime = serverTime;
}
}
主要回应Class
public class TranslateResp {
@Expose
private List<Sentence> sentences = new ArrayList<Sentence>();
public List<Sentence> getSentences() {
return sentences;
}
public void setSentences(List<Sentence> sentences) {
this.sentences = sentences;
}
}
这是 JSON GEN 的另一个好东西 - 客户端代码生成工具 http://jsongen.byingtondesign.com/