java - Deserialize jackson in spring boot -


i getting 2 types of json calling resttemplate

{"results":[{"testing":{"name":"soham"}},{"testing":{"firstname":"john","lastname":"don"}}]} 

now parsing data using jsondeserializer

public class customjacksondeserialize extends jsondeserializer<activity> {     @override     public activity deserialize(jsonparser jsonparser, deserializationcontext deserializationcontext) throws             ioexception, nullpointerexception {         objectmapper objectmapper = new objectmapper();         jsonnode actualobj = objectmapper.readtree(jsonparser.getvalueasstring());              return new activity(actualobj.get("name").astext());     } } 

here class

@jsondeserialize(using = customjacksondeserialize.class) public class activity {      private string name;      public activity(string name) {         this.name = name;     } } 

now question how parse json.@jsondeserialize isn't helpful in case.any other options?or how use @jsondeserialize in case.

you can modify customjacksondeserialize :

class customjacksondeserialize extends jsondeserializer<activity> {     @override     public activity deserialize(jsonparser jsonparser, deserializationcontext deserializationcontext) throws ioexception,             nullpointerexception {          jsonnode jsonnode = jsonparser.readvalueastree();          string name=jsonnode.get("results").get(0).get("testing").get("name").astext();          return new activity(name);     } }  

second approach: alternatively if can de-serilaize json using object mapper not require customjacksondeserialize :

string jsonstring = "{\"results\":[{\"testing\":{\"name\":\"soham\"}},{\"testing\":{\"firstname\":\"john\",\"lastname\":\"don\"}}]}";     objectmapper objectmapper = new objectmapper();  jsonnode jsonnode = objectmapper.readtree(jsonstring); jsonnode resultnode=jsonnode.get("results"); string name = resultnode.get(0).get("testing").get("name").astext();  activity activity=new activity(name);  string firstname=  resultnode.get(1).get("testing").get("firstname").astext(); string lastname=  resultnode.get(1).get("testing").get("lastname").astext();  activity2 activity2=new activity2(firstname, lastname); 

note : have extracted value of name, firstname , lastname given json, can modify logic accordingly.

third approach: here can iterate through json array , create separate objects of both activities:

string jsonstring = "{\"results\":[{\"testing\":{\"name\":\"soham\"}},{\"testing\":{\"firstname\":\"john\",\"lastname\":\"don\"}}]}";          objectmapper objectmapper = new objectmapper();          jsonnode jsonnode = objectmapper.readtree(jsonstring);          jsonnode resultnode = jsonnode.get("results");          list<activity> activitylist1 = new arraylist<activity>();          list<activity2> activitylist2 = new arraylist<activity2>();          (int = 0; < resultnode.size(); i++) {              jsonnode testingnode = resultnode.get(i).get("testing");              if (testingnode.has("name")) {                 string name = testingnode.get("name").astext();                  activity activity = new activity(name);                 activitylist1.add(activity);              }              if (testingnode.has("firstname")) {                 string firstname = testingnode.get("firstname").astext();                 string lastname = testingnode.get("lastname").astext();                  activity2 activity2 = new activity2(firstname, lastname);                 activitylist2.add(activity2);              }          } 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -