Java class from json with same name -


i have json object following:

...     {        "url": "checkout.bodenusa.com/en-us"     },     {        "url": [             ".bonton.com/checkout/",             ".bonton.com/checkoutview"         ]     } ... 

how should java class response server.

i try snippet, incorrect:

@serializedname("url") @expose private list<string> urllist = null; @serializedname("url") @expose private string url; 

create model

import com.google.gson.annotations.expose; import com.google.gson.annotations.serializedname;  import java.util.list;  public class model {      @serializedname("url")     @expose     private list<string> url = null;     @serializedname("apply")     @expose     private apply apply;     @serializedname("controls")     @expose     private controls controls;     @serializedname("remove")     @expose     private remove remove;      public list<string> geturl() {         return url;     }      public void seturl(list<string> url) {         this.url = url;     }      public apply getapply() {         return apply;     }      public void setapply(apply apply) {         this.apply = apply;     }      public controls getcontrols() {         return controls;     }      public void setcontrols(controls controls) {         this.controls = controls;     }      public remove getremove() {         return remove;     }      public void setremove(remove remove) {         this.remove = remove;     }       public class controls {          @serializedname("promo")         @expose         private string promo;         @serializedname("total")         @expose         private string total;         @serializedname("ordertotal")         @expose         private string ordertotal;         @serializedname("coupon")         @expose         private string coupon;          public string getpromo() {             return promo;         }          public void setpromo(string promo) {             this.promo = promo;         }          public string gettotal() {             return total;         }          public void settotal(string total) {             this.total = total;         }          public string getordertotal() {             return ordertotal;         }          public void setordertotal(string ordertotal) {             this.ordertotal = ordertotal;         }          public string getcoupon() {             return coupon;         }          public void setcoupon(string coupon) {             this.coupon = coupon;         }      }      public class remove {          @serializedname("type")         @expose         private string type;         @serializedname("submit")         @expose         private string submit;         @serializedname("timeout")         @expose         private integer timeout;          public string gettype() {             return type;         }          public void settype(string type) {             this.type = type;         }          public string getsubmit() {             return submit;         }          public void setsubmit(string submit) {             this.submit = submit;         }          public integer gettimeout() {             return timeout;         }          public void settimeout(integer timeout) {             this.timeout = timeout;         }      }      public class apply {          @serializedname("type")         @expose         private string type;         @serializedname("submit")         @expose         private string submit;         @serializedname("timeout")         @expose         private integer timeout;          public string gettype() {             return type;         }          public void settype(string type) {             this.type = type;         }          public string getsubmit() {             return submit;         }          public void setsubmit(string submit) {             this.submit = submit;         }          public integer gettimeout() {             return timeout;         }          public void settimeout(integer timeout) {             this.timeout = timeout;         }      }  } 

use class along custom typeadapter gson .then work both list , object response .

arrayadapter class

import com.google.gson.gson; import com.google.gson.typeadapter; import com.google.gson.typeadapterfactory; import com.google.gson.reflect.typetoken;  import java.lang.reflect.parameterizedtype; import java.util.arraylist; import java.util.list;  public class arrayadapterfactory implements typeadapterfactory {      @override     @suppresswarnings({"unchecked", "rawtypes"})     public <t> typeadapter<t> create(final gson gson, final typetoken<t> type) {          typeadapter<t> typeadapter = null;         try {             if (type.getrawtype() == list.class || type.getrawtype() == arraylist.class) {                  typeadapter = new arrayadapter(gson,                         (class) ((parameterizedtype) type.gettype())                                 .getactualtypearguments()[0]);             }         } catch (exception e) {             e.printstacktrace();         }          return typeadapter;      }  } 

arrayadapterfactory class

import com.google.gson.gson; import com.google.gson.typeadapter; import com.google.gson.stream.jsonreader; import com.google.gson.stream.jsontoken; import com.google.gson.stream.jsonwriter;  import java.io.ioexception; import java.util.arraylist; import java.util.list;   class arrayadapter<t> extends typeadapter<list<t>> {      private class<t> adapterclass;     private gson gson;      public arrayadapter(gson gson, class<t> adapterclass) {         this.adapterclass = adapterclass;         this.gson = gson;     }      @override     public list<t> read(jsonreader reader) throws ioexception {          list<t> list = new arraylist<t>();          final jsontoken token = reader.peek();         system.out.println(token);         // handling of scenario 2( check javadoc class) :         if (token == jsontoken.string || token == jsontoken.number ||                 token == jsontoken.boolean) {             t inning = (t) gson.fromjson(reader, adapterclass);             list.add(inning);         } else if (token == jsontoken.begin_object) {             // handling of scenario 1(check javadoc class) :             t inning = (t) gson.fromjson(reader, adapterclass);             list.add(inning);         } else if (token == jsontoken.begin_array) {             reader.beginarray();             while (reader.hasnext()) {                 @suppresswarnings("unchecked")                 t inning = (t) gson.fromjson(reader, adapterclass);                 list.add(inning);             }             reader.endarray();         }          return list;     }      @override     public void write(jsonwriter writer, list<t> value) throws ioexception {      }   } 

and register adapter factory this,

gson gson  = new gsonbuilder().registertypeadapterfactory(new arrayadapterfactory()).create(); 

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 -