Java – GSON Expected BEGIN_ARRAY but was BEGIN_OBJECT

androidgsonjavaweb services

I just start to learn about gson.
I see lot of topic about this error, but i don't find any help or answer corresponding of my problem.

I'm trying to parse a JSON string like this 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 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
}

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

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

But it throws me with

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

Any ideas how should I fix it?

Thanks!

EDIT

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)

EDIT 2

The problem come from the two 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 i comment this properties i don't have any probleme to build my json objet.

I found this link Deserializing a Map<String, Object> field with Gson

Currently i try something like 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();

        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;
    }
}

Do you think i'm in the good way to resolve my problem ?

Best Answer

Upgrade to the latest Gson and it'll tell you exactly where there's a mismatch. Typically this happens when your model has a List but your JSON doesn't (ie. no [...]).

Related Topic