java - deserialize arrayList of objects in jakson -
currently facing problem how de serialize list of objects in json format pojo. in works using jersey rest service can consume json. how can de-serialize rest request 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); } }
Comments
Post a Comment