GSON预计BEGIN_ARRAY但是BEGIN_OBJECT?

我刚开始学习gson。 我看到很多关于这个错误的话题,但我没有找到任何与我的问题相对应的帮助或答案。

我正在尝试解析像这样的JSON字符串:

{ "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 at 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 } 

成对象

 InfoSecuriteBean mInfoSecuriteBean = gson.fromJson(reader, InfoSecuriteBean.class); 

这是我正在使用的对象类。

 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 mRoleSGBD; @SerializedName("RoleSI") private List 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> mHabilitationAD = new HashMap<String, List>(); @SerializedName("HabilitationInterprete") private Map<String,List> mHabilitationInterprete = new HashMap<String, List>(); //Getter and setter 

和另一个对象使用

 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 and setter 

但它让我感到高兴

 com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECTat line 1 column 60 

任何想法我该如何解决?

谢谢!

编辑

 Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 60 path $.HabilitationAD[0] at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:200) 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) Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 60 path $.HabilitationAD[0] at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350) at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:172) at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:145) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:103) at 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) 

编辑2

问题来自两个HashMap>

  @SerializedName("HabilitationAD") private Map<String,List> mHabilitationAD = new HashMap<String, List>(); @SerializedName("HabilitationInterprete") private Map<String,List> mHabilitationInterprete = new HashMap<String, List>(); 

如果我评论这个属性我没有任何问题来构建我的json objet。

我发现这个链接用Gson反序列化一个Map 字段

目前我尝试这样的事情

 public class InfoSecuriteBeanDeserializer implements JsonDeserializer { @Override public InfoSecuriteBean deserialize(JsonElement json, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { JsonArray jArray = (JsonArray) json; InfoSecuriteBean mInfoSecuriteBean = new InfoSecuriteBean(); for (int i=1; i<jArray.size(); i++) { JsonObject jObject = (JsonObject) jArray.get(i); //assuming you have the 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; } } 

你认为我是解决问题的好方法吗?

升级到最新的Gson,它会告诉你哪里有不匹配的地方。 通常,当您的模型具有List但您的JSON没有(即没有[...] )时会发生这种情况。

我找到了解决问题的简单解决方案。 我将在这里展示我的解决方案,也许这将有助于一些人。

所以我创建了两个新类:HabilitationAD和HabilitationInterprete

 public class HabilitationAD { @SerializedName("Key") private String mKey; @SerializedName("Value") private List mValue; public String getmKey() { return mKey; } public void setmKey(String mKey) { this.mKey = mKey; } public List getmValue() { return mValue; } public void setmValue(List mValue) { this.mValue = mValue; } public HabilitationAD(){ } public HabilitationAD(String p_mKey, List p_mValue){ this.mKey = p_mKey; this.mValue = p_mValue; } } 

并且:

  public class HabilitationInterprete { @SerializedName("Key") private String mKey; @SerializedName("Value") private List mValue; public String getmKey() { return mKey; } public void setmKey(String mKey) { this.mKey = mKey; } public List getmValue() { return mValue; } public void setmValue(List mValue) { this.mValue = mValue; } public HabilitationInterprete(){ } public HabilitationInterprete(String p_mKey, List p_mValue){ this.mKey = p_mKey; this.mValue = p_mValue; } 

我修改了我的类InfoSecuriteBean

我改变了这个:

  @SerializedName("HabilitationAD") private Map> mHabilitationAD = new HashMap>(); @SerializedName("HabilitationInterprete") private Map> mHabilitationInterprete = new HashMap>(); 

这样 :

 @SerializedName("HabilitationAD") private List habilitationAD; @SerializedName("HabilitationInterprete") private List habilitationInterprete; 

现在一切都很棒! 希望这个解决方案能有所帮

你得到的错误非常标准。 Gson无法反序列化嵌套对象。 要写自己的适配器。

因此,您必须学习如何编写自定义反序列化器(非常简单),然后在创建gson对象时注册它。

随意提出更多问题。