我有一个Android应用程序使用Jackson来反序列化数据,我在为这个json字符串创建pojo时遇到麻烦:
{
"response": [{
"view": "ticker",
"items": []
}, {
"view": "note",
"note": "This is a note"
}, {
"wn": "bla",
"sd": "bla bla",
"tf": 28,
"rh": 22,
"ws": 9,
"ti": "14:00",
"view": "hbhi"
}]
}
我创建了以下pojos:
TickerModel.java
public class TickerModel implements Serializable {
@JsonProperty("view")
private String view ;
@JsonProperty("items")
private String items;
public String getView() {
return view;
}
public void setView(String view) {
this.view = view;
}
public String getItems() {
return items;
}
public void setItems(String items) {
this.items = items;
}
}
NoteModel.java
public class NoteModel implements Serializable {
@JsonProperty("view")
private String view ;
@JsonProperty("note")
private String note;
public String getView() {
return view;
}
public void setView(String view) {
this.view = view;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
}
有关如何使用Jackson对此JSON进行反序列化的任何想法?
答案 0 :(得分:0)
答案 1 :(得分:0)
不需要创建两个单独的pojo,你可以像下面这样的pojo -
public class Response {
@JsonProperty("view")
private String view;
@JsonProperty("items")
private List<Object> items = new ArrayList<Object>();
@JsonProperty("note")
private String note;
@JsonProperty("wn")
private String wn;
@JsonProperty("sd")
private String sd;
@JsonProperty("tf")
private Integer tf;
@JsonProperty("rh")
private Integer rh;
@JsonProperty("ws")
private Integer ws;
@JsonProperty("ti")
private String ti;
//setters //getters
}
注意:由于items是json中的数组,因此应将其映射为类型对象的数组列表。如果你有json数据,你也可以在线玩pojo - http://www.jsonschema2pojo.org/