我無法將這部分JSON轉換爲POJO。我使用的是傑克遜的配置是這樣的:將JSON列表轉換爲POJO
protected ThreadLocal<ObjectMapper> jparser = new ThreadLocal<ObjectMapper>();
public void receive(Object object) {
try {
if (object instanceof String && ((String)object).length() != 0) {
ObjectDefinition t = null ;
if (parserChoice==0) {
if (jparser.get()==null) {
jparser.set(new ObjectMapper());
}
t = jparser.get().readValue((String)object, ObjectDefinition.class);
}
Object key = t.getKey();
if (key == null)
return;
transaction.put(key,t);
}
} catch (Exception e) {
e.printStackTrace();
}
}
下面是需要變成一個POJO的JSON:
{
"id":"exampleID1",
"entities":{
"tags":[
{
"text":"textexample1",
"indices":[
2,
14
]
},
{
"text":"textexample2",
"indices":[
31,
36
]
},
{
"text":"textexample3",
"indices":[
37,
43
]
}
]
}
最後,這是我目前擁有的Java類:
protected Entities entities;
@JsonIgnoreProperties(ignoreUnknown = true)
protected class Entities {
public Entities() {}
protected Tags tags;
@JsonIgnoreProperties(ignoreUnknown = true)
protected class Tags {
public Tags() {}
protected String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
};
public Tags getTags() {
return tags;
}
public void setTags(Tags tags) {
this.tags = tags;
}
};
//Getters & Setters ...
我已經能夠將更簡單的對象轉換爲POJO,但列表讓我難倒了。
任何幫助表示讚賞。謝謝!
http://stackoverflow.com/questions/9829403/deserialize-json-to-arraylistpojo-using-jackson – mre