java - GSON Expected BEGIN_ARRAY but was BEGIN_OBJECT? -


i start learn gson. see lot of topic error, don't find or answer corresponding of problem.

i'm trying parse json string one:

{ "codeanalytique": "xxxx",   "domaine": "xxxx",   "habilitationad": [     {       "key": "xxxx",       "value": [         {           "applicatif": "xxxx",           "cle": "e",           "role": "red",           "valeur": "xxxx"         },         {           "applicatif": "xxxx",           "cle": "e",           "role": "red",           "valeur": "xxxx"         }         //lot of other value         ]     }   ],   "habilitationinterprete": [     {       "key": "xx",       "value": [         {           "applicatif": "xxxx",           "cle": "z",           "role": "red",           "valeur": "xxx"         },         {           "applicatif": "xxxx",           "cle": "z",           "role": "red",           "valeur": "xxxx"         }         //lot of other value         ]     }   ],   "identity": "xxxx",   "infoconsolidee": "operation requested @ xxxx",   "isadmini": true,   "isadminm": false,   "isauthentif": true,   "matricule": "xxx",   "nom": "xxxx",   "passwordenvoye": "xxxx",   "prenom": "xxx",   "principalpermissionmode": 1,   "rolesgbd": [     "xxxx"   ],   "rolesi": [     null,     "app_02",     "app_03",    //lot of other value   ],   "societe": "xxx",   "typeclient": null } 

into objects

infosecuritebean minfosecuritebean = gson.fromjson(reader, infosecuritebean.class); 

here's object class i'm using.

import com.google.gson.annotations.serializedname;  import java.util.hashmap; import java.util.list; import java.util.map;  public class infosecuritebean {      @serializedname("isauthentif")     private boolean misauthentif = false;      @serializedname("principalpermissionmode")     private enumprincipalpermissionmode mprincipalpermissionmode;     @serializedname("rolesgbd")     private list<string> mrolesgbd;     @serializedname("rolesi")     private list<string> mrolesi;     @serializedname("identity")     private string midentity = null;     @serializedname("password")     private string mpassword = null;     @serializedname("infoconsolidee")     private string minfoconsolidee = null;      @serializedname("isadminm")     private boolean misadminm = false;     @serializedname("isadmini")     private boolean misadmini = false;      @serializedname("matricule")     private string mmatricule = null;     @serializedname("nom")     private string mnom = null;     @serializedname("prenom")     private string mprenom = null;     @serializedname("codeanalytique")     private string mcodeanalytique = null;     @serializedname("domaine")     private string mdomaine = null;     @serializedname("societe")     private string msociete = null;     @serializedname("typeclient")     private string mtypeclient = null;      @serializedname("habilitationad")     private map<string,list<habilitationbean>> mhabilitationad = new hashmap<string, list<habilitationbean>>();     @serializedname("habilitationinterprete")     private map<string,list<habilitationbean>> mhabilitationinterprete = new hashmap<string, list<habilitationbean>>(); //getter , setter 

and other object use

import com.google.gson.annotations.serializedname;  public class habilitationbean {     @serializedname("applicatif")     private string mapplicatif = null;     @serializedname("role")     private string mrole = null;     @serializedname("cle")     private string mcle = null;     @serializedname("valeur")     private string mvaleur = null;  //getter , setter 

but throws me with

com.google.gson.jsonsyntaxexception: java.lang.illegalstateexception: expected begin_array begin_objectat line 1 column 60 

any ideas how should fix it?

thanks!

edit

caused by: com.google.gson.jsonsyntaxexception: java.lang.illegalstateexception: expected begin_array begin_object @ line 1 column 60 path $.habilitationad[0]             @ com.google.gson.internal.bind.reflectivetypeadapterfactory$adapter.read(reflectivetypeadapterfactory.java:200)             @ com.google.gson.gson.fromjson(gson.java:810)             @ com.google.gson.gson.fromjson(gson.java:748)             @ com.sig.webservice.websecuritygbd.doinbackground(websecuritygbd.java:69)             @ com.sig.webservice.websecuritygbd.doinbackground(websecuritygbd.java:23)             @ android.os.asynctask$2.call(asynctask.java:292)             @ java.util.concurrent.futuretask.run(futuretask.java:237)             at android.os.asynctask$serialexecutor$1.run(asynctask.java:231)             at java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112)             at java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587)             at java.lang.thread.run(thread.java:818)      caused by: java.lang.illegalstateexception: expected begin_array begin_object @ line 1 column 60 path $.habilitationad[0]             @ com.google.gson.stream.jsonreader.beginarray(jsonreader.java:350)             @ com.google.gson.internal.bind.maptypeadapterfactory$adapter.read(maptypeadapterfactory.java:172)             @ com.google.gson.internal.bind.maptypeadapterfactory$adapter.read(maptypeadapterfactory.java:145)             @ com.google.gson.internal.bind.reflectivetypeadapterfactory$1.read(reflectivetypeadapterfactory.java:103)             @ com.google.gson.internal.bind.reflectivetypeadapterfactory$adapter.read(reflectivetypeadapterfactory.java:196)             at com.google.gson.gson.fromjson(gson.java:810)             at com.google.gson.gson.fromjson(gson.java:748)             at com.sig.webservice.websecuritygbd.doinbackground(websecuritygbd.java:69)             at com.sig.webservice.websecuritygbd.doinbackground(websecuritygbd.java:23)             at android.os.asynctask$2.call(asynctask.java:292)             at java.util.concurrent.futuretask.run(futuretask.java:237)             at android.os.asynctask$serialexecutor$1.run(asynctask.java:231)             at java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112)             at java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587)             at java.lang.thread.run(thread.java:818) 

edit 2

the problem come 2 hashmap>

 @serializedname("habilitationad")         private map<string,list<habilitationbean>> mhabilitationad = new hashmap<string, list<habilitationbean>>();         @serializedname("habilitationinterprete")         private map<string,list<habilitationbean>> mhabilitationinterprete = new hashmap<string, list<habilitationbean>>();  

if comment properties don't have probleme build json objet.

i found link deserializing map<string, object> field gson

currently try this

public class infosecuritebeandeserializer implements jsondeserializer<infosecuritebean> {      @override     public infosecuritebean deserialize(jsonelement json, type type, jsondeserializationcontext jsondeserializationcontext) throws jsonparseexception {          jsonarray jarray = (jsonarray) json;          infosecuritebean minfosecuritebean = new infosecuritebean();          (int i=1; i<jarray.size(); i++) {             jsonobject jobject = (jsonobject) jarray.get(i);             //assuming have suitable constructor...             habilitationbean mhabilitationbean = new habilitationbean(jobject.get("applicatif").getasstring(),                     jobject.get("cle").getasstring(),                     jobject.get("role").getasstring(),                     jobject.get("valeur").getasstring());             minfosecuritebean.getmhabilitationad().add(mhabilitationbean);         }          return minfosecuritebean;     } } 

do think i'm in way resolve problem ?

i found solution solve problem. show here solution, maybe people.

so create 2 new class : habilitationad , habilitationinterprete

public class habilitationad {      @serializedname("key")     private string mkey;      @serializedname("value")     private list<beanhabilitation> mvalue;      public string getmkey() {         return mkey;     }      public void setmkey(string mkey) {         this.mkey = mkey;     }      public list<beanhabilitation> getmvalue() {         return mvalue;     }      public void setmvalue(list<beanhabilitation> mvalue) {         this.mvalue = mvalue;     }      public habilitationad(){      }      public habilitationad(string p_mkey, list<beanhabilitation> p_mvalue){         this.mkey = p_mkey;         this.mvalue = p_mvalue;     } } 

and :

    public class habilitationinterprete {          @serializedname("key")         private string mkey;          @serializedname("value")         private list<beanhabilitation> mvalue;          public string getmkey() {             return mkey;         }          public void setmkey(string mkey) {             this.mkey = mkey;         }          public list<beanhabilitation> getmvalue() {             return mvalue;         }          public void setmvalue(list<beanhabilitation> mvalue) {             this.mvalue = mvalue;         }   public habilitationinterprete(){      }      public habilitationinterprete(string p_mkey, list<beanhabilitation> p_mvalue){         this.mkey = p_mkey;         this.mvalue = p_mvalue;     } 

and modify class infosecuritebean

i changed :

 @serializedname("habilitationad")     private map<string,list<habilitationbean>> mhabilitationad = new hashmap<string, list<habilitationbean>>();     @serializedname("habilitationinterprete")     private map<string,list<habilitationbean>> mhabilitationinterprete = new hashmap<string, list<habilitationbean>>(); 

by :

@serializedname("habilitationad")     private list<habilitationad> habilitationad;     @serializedname("habilitationinterprete")     private list<habilitationinterprete> habilitationinterprete; 

now work great ! hope solution !


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -