android - Gson Deserialization of Mixed Object types -


i have json:

"account_representatives": [       {         "sales person": 1307,         "default_ticket_assignments": [           ""         ],         "primary account manager": 1307,         "secondary support-3": 1151,         "relationship mgr": 1307,         "authorized_resources": [           ""         ],         "technical account manager": 164       }     ] 

and have class structure this:

public class accountrepresentative {          @serializedname("authorized_resources")         @expose         private list<string> authorizedresources = new arraylist<string>();         @serializedname("default_ticket_assignments")         @expose         private list<string> defaultticketassignments = new arraylist<string>();         @expose         private map<string, string> repfields;          /**          * @return authorizedresources          */         public list<string> getauthorizedresources() {             return authorizedresources;         }          /**          * @param authorizedresources authorized_resources          */         public void setauthorizedresources(list<string> authorizedresources) {             this.authorizedresources = authorizedresources;         }          /**          * @return defaultticketassignments          */         public list<string> getdefaultticketassignments() {             return defaultticketassignments;         }          /**          * @param defaultticketassignments default_ticket_assignments          */         public void setdefaultticketassignments(list<string> defaultticketassignments) {             this.defaultticketassignments = defaultticketassignments;         }          /**          *          * @return dynamic jsonobjects          */         public map<string, string> getrepfields() {             return repfields;         }          /**          * @param repfields repfields          */         public void setrepfields(map<string, string> repfields) {             this.repfields = repfields;         }     } 

i'm using gson bind these, i'm able bind 2 jsonarrays.

i can deserialized dynamic properties w/o jsonarrays using how can convert json hashmap using gson?,

but in case, have no idea how dynamic properties, plan supposedly turn dynamic properties hashmap, 2 jsonarrays in way.

thanks help,

i have found out how use gson custom deserializer post, using gson parse array multiple types, post, java collections - keyset() vs entryset() in map did create custom deserializer:

package com.xxx.xyz.models;  import android.util.log;  import com.google.gson.jsonarray; import com.google.gson.jsondeserializationcontext; import com.google.gson.jsondeserializer; import com.google.gson.jsonelement; import com.google.gson.jsonobject; import com.google.gson.jsonparseexception;  import java.lang.reflect.type; import java.util.arraylist; import java.util.hashmap; import java.util.iterator; import java.util.map; import java.util.map.entry; import java.util.set;  public class accountrepresentativedeserializer implements jsondeserializer<accountdetailsmodel.accountrepresentative> {     @override     public accountdetailsmodel.accountrepresentative deserialize(jsonelement json, type typeoft, jsondeserializationcontext context) throws jsonparseexception {         log.v("authorized rep ", json.tostring());         accountdetailsmodel.accountrepresentative accountrep = new accountdetailsmodel().new accountrepresentative();         jsonobject jobject = json.getasjsonobject();         set<entry<string, jsonelement>> items = jobject.entryset();         iterator<entry<string, jsonelement>> = items.iterator();         map<string, string> repfields = new hashmap<>();          while (it.hasnext()) {             entry<string, jsonelement> = it.next();             if (i.getkey().equals("default_ticket_assignments")) {                 arraylist<string> defaultticketassignments = new arraylist<>();                 jsonarray defticketassignment = i.getvalue().getasjsonarray();                 iterator<jsonelement> defticketiterator = defticketassignment.iterator();                  while (defticketiterator.hasnext()) {                     jsonelement defticketitem = defticketiterator.next();                     defaultticketassignments.add(defticketitem.getasstring());                 }                 accountrep.setdefaultticketassignments(defaultticketassignments);             } else if (i.getkey().equals("authorized_resources")) {                 arraylist<string> authorizedresources = new arraylist<>();                 jsonarray authorizedres = i.getvalue().getasjsonarray();                 iterator<jsonelement> authorizedresiterator = authorizedres.iterator();                  while (authorizedresiterator.hasnext()) {                     jsonelement defticketitem = authorizedresiterator.next();                     authorizedresources.add(defticketitem.getasstring());                 }                 accountrep.setauthorizedresources(authorizedresources);             } else {                 repfields.put(i.getkey(), i.getvalue().getasstring());             }         }         accountrep.setrepfields(repfields);         return accountrep;     } } 

then use so:

gson gson = new gsonbuilder().registertypeadapter(accountdetailsmodel.accountrepresentative.class, new accountrepresentativedeserializer()).create();                         accountdetailsmodel result = gson.fromjson(strresult, accountdetailsmodel.class); 

btw, accountrepresentative inner class , 2 arrays inside accountrepresentative in json, properties dynamic.

i thought won't able parse internal 1 , make custom deserializer accountdetailmodel itself, tried create deserializer accountrepresentative, , went fine :)


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -