java - How to parse a JSON string to an list using Jackson -


i have string following value:

{   "keya": {     "id": "123",     "name": "testa",     "mobile": "1111"   },   "keyb": {     "id": "456",     "name": "testb",     "mobile": "2222"   } } 

how convert json list , class formats

and want parse list

please advise how achieve using jackson objectmapper?

you try testing:

public static void main(string[] args) {     string jsonstring = "{\n" +                 "  \"keya\": {\n" +                 "    \"id\": \"123\",\n" +                 "    \"name\": \"testa\",\n" +                 "    \"mobile\": \"1111\"\n" +                 "  },\n" +                 "  \"keyb\": {\n" +                 "    \"id\": \"456\",\n" +                 "    \"name\": \"testb\",\n" +                 "    \"mobile\": \"2222\"\n" +                 "  }\n" +                 "}";     list<customobject> customobjects = new arraylist<customobject>();     objectmapper mapper = new objectmapper();     try {         classa myclassaobject= mapper.readvalue(jsonstring, classa.class);         customobjects.add(myclassaobject.getkeya());         customobjects.add(myclassaobject.getkeyb());     }     catch(exception e) {         e.printstacktrace();     }     system.out.println(customobjects.size()); } 

my other model classes, customobject:

public class customobject {      private string id;     private string name;     private string mobile;      @jsonproperty("id")     public string getid() {         return id;     }      @jsonproperty("id")     public void setid(string id) {         this.id = id;     }      @jsonproperty("name")     public string getname() {         return name;     }      @jsonproperty("name")     public void setname(string name) {         this.name = name;     }      @jsonproperty("mobile")     public string getmobile() {         return mobile;     }      @jsonproperty("mobile")     public void setmobile(string mobile) {         this.mobile = mobile;     } } 

and classa

public class classa {      private customobject keya;     private customobject keyb;      public customobject getkeya() {         return keya;     }      public void setkeya(customobject keya) {         this.keya = keya;     }      public customobject getkeyb() {         return keyb;     }      public void setkeyb(customobject keyb) {         this.keyb = keyb;     } } 

in case have issues, library version used in maven:

<dependency>     <groupid>com.fasterxml.jackson.core</groupid>     <artifactid>jackson-databind</artifactid>     <version>2.4.1.3</version> </dependency> 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -