我无法将这部分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