How to deserialize JSON array of list elements in Java? -


i have given json:

    {   "facility-no": "2011",   "standard-counter": [     {       "id": "0",       "type": "0",       "text": "gebucht",       "free": "0",       "present": "0",       "percent": "100",       "max": "0",       "status": "frei",       "status-value": "0"     }, ... ], ... } 

and want deserialize classes...

a wrapper class:

public class counters {      @jsonproperty("facility-no")     private string facilityid;      @jsonproperty("standard-counter")     private list<xcounter> xcounters; } 

the class implementing object kept list in wrapper class:

public class xcounter {      protected string id;     @jsonignore     public static final countertypeenum type = countertypeenum.x_counter;      // standard & level counter properties     @jsonserialize(include=jsonserialize.inclusion.non_null)     string text;     @jsonserialize(include=jsonserialize.inclusion.non_null)     int free;     @jsonserialize(include=jsonserialize.inclusion.non_null)     int present;     @jsonserialize(include=jsonserialize.inclusion.non_null)     float percent;     @jsonserialize(include=jsonserialize.inclusion.non_null)     int max;     @jsonserialize(include=jsonserialize.inclusion.non_null)     string status;     @jsonserialize(include=jsonserialize.inclusion.non_null)     @jsonproperty("status-value")     int statusvalue;     ...(all getters , setters...) 

here's countertypeenum:

public enum countertypeenum {     x_counter(0), y_counter(1), z_counter(2);     private int type;     private countertypeenum(final int type) {         this.type = type;     }     public int gettype() {         return this.type;     } } 

however, unrecognizedpropertyexception:

org.codehaus.jackson.map.exc.unrecognizedpropertyexception: unrecognized field "type" (class com.foo.bar.xcounter), not marked ignorable  @ [source: java.io.stringreader@42077608; line: 1, column: 61] (through reference chain: com.foo.bar.counters["standard-counter"]->com.foo.bar.xcounter["type"]) 

if not use @jsonignoreproperties(ignoreunknown=true) on class level of substandardcounters.

how can avoid exception without using @jsonignoreproperties(ignoreunknown=true)?

you can consider replacing enum type actual class type leveraging the jackson's polymorphic deserialization. in case jackson create instance of type depending on value of type property.

here example:

public class jacksonpolymorphic {     public static string json = "{\n" +             "  \"facility-no\": \"2011\",\n" +             "  \"standard-counter\": [\n" +             "    {\n" +             "      \"id\": \"0\",\n" +             "      \"type\": \"0\",\n" +             "      \"text\": \"gebucht\",\n" +             "      \"free\": \"0\",\n" +             "      \"present\": \"0\",\n" +             "      \"percent\": \"100\",\n" +             "      \"max\": \"0\",\n" +             "      \"status\": \"frei\",\n" +             "      \"status-value\": \"0\"\n" +             "    }\n" +             "]\n" +             "}";      @jsontypeinfo(use = jsontypeinfo.id.name, property = "type")     public static abstract class counter {         private final string id;          public counter(string id) {             this.id = id;         }     }      public static class counters {         private final string facilityid;         private final list<counter> counters;          public counters(@jsonproperty("facility-no") string facilityid,                         @jsonproperty("standard-counter") list<counter> counters) {             this.facilityid = facilityid;             this.counters = counters;         }          @override         public string tostring() {             return "counters{" +                     "facilityid='" + facilityid + '\'' +                     ", counters=" + counters +                     '}';         }     }      @jsontypename("0")     public static class xcounter extends counter {         private final string text;         private final int free;         private final int present;         private final float percent;         private final int max;         private final string status;         private final int statusvalue;          public xcounter(@jsonproperty("id") string id,                         @jsonproperty("text") string text,                         @jsonproperty("free") int free,                         @jsonproperty("present") int present,                         @jsonproperty("percent") float percent,                         @jsonproperty("max") int max,                         @jsonproperty("status") string status,                         @jsonproperty("status-value") int statusvalue) {             super(id);             this.text = text;             this.free = free;             this.present = present;             this.percent = percent;             this.max = max;             this.status = status;             this.statusvalue = statusvalue;         }          @override         public string tostring() {             return "xcounter{" +                     "text='" + text + '\'' +                     ", free=" + free +                     ", present=" + present +                     ", percent=" + percent +                     ", max=" + max +                     ", status='" + status + '\'' +                     ", statusvalue=" + statusvalue +                     '}';         }     }      public static void main(string[] args) throws ioexception {         objectmapper mapper = new objectmapper();         mapper.registersubtypes(xcounter.class);         system.out.println(mapper.readvalue(json, counters.class));     }  } 

output:

counters{facilityid='2011', counters=[xcounter{text='gebucht', free=0, present=0, percent=100.0, max=0, status='frei', statusvalue=0}]} 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -