currently an facing a problem how to de serialize list of objects in json format to pojo. in my works using jersey rest service it can consume json. how can de-serialize rest request with json object having array of objects .
json array
{
"subject": "hi",
"description": [
{
"subject": "hi"
},
{
"subject": "hi"
}
]
}
my pojo class
public class t {
private String subject;
private List<t2> description;
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public List<t2> getDescription() {
return description;
}
public void setDescription(List<t2> description) {
this.description = description;
}
}
t2.class
public class t2 {
private String subject;
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
}
Use TypeReference
. Eg:
import java.io.IOException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonParser {
public static void main(String[] args) {
t data = null;
String json = "{\"subject\":\"hi\",\"description\":[{\"subject\":\"hi\"},{\"subject\":\"hi\"}]}";
ObjectMapper mapper = new ObjectMapper();
try {
data = mapper.readValue(json, new TypeReference<t>() { });
} catch (JsonParseException e) {
System.out.println(e.getMessage());
} catch (JsonMappingException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
System.out.println(data);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.