GSON预计BEGIN_ARRAY,但是BEGIN_OBJECT? [英] GSON Expected BEGIN_ARRAY but was BEGIN_OBJECT?
问题描述
我刚开始学习gson。
我看到很多关于这个错误的话题,但我没有找到任何帮助或答案,相应的我的问题。
我试图解析JSON字符串就像这样:
{
CodeAnalytique:XXXX,
Domaine :XXXX,
HabilitationAD:[
{
Key:XXXX,
Value:[
{
应用程序:XXXX,
Cle:E,
角色:红色,
Valeur:XXXX
},
$ Applicatif:XXXX
Cle $:E
角色:红色
Valeur:XXXX
}
//很多其他值
]
}
],
HabilitationInterprete:[
{
Key:XX,
Value:[
{
Applicatif:XXXX,
Cle:Z,
角色:红色,
Valeur:XXX
},
{
Applicatif:XXXX,
Cle:Z,
角色:红色,
Valeur:XXXX
}
//很多其他值
$ b $身份:XXXX
InfoConsolidee:在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,
//很多其他值
],
Societe:XXX,
TypeClient:null
}
转换为对象
InfoSecuriteBean mInfoSecuriteBean = gson.fromJson(reader,InfoSecuriteBean 。类);
这里是我使用的对象类。
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)
私人列表< String> mRoleSGBD;
@SerializedName(RoleSI)
私人列表< 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)
私人地图< String,List< HabilitationBean>> mHabilitationAD = new HashMap< String,List< HabilitationBean>>();
@SerializedName(HabilitationInterprete)
私人地图< String,List< HabilitationBean>> mHabilitationInterprete = new HashMap< String,List< HabilitationBean>>();
// Getter和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:预期的BEGIN_ARRAY,但是BEGIN_OBJECTat行1列60
任何想法我应该如何解决它?
谢谢! p>
编辑
google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期的BEGIN_ARRAY,但是在第1行第60列的BEGIN_OBJECT $ .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)
at com.sig.webservice.WebSecurityGBD.doInBackground(WebSecurityGBD.java:69)
at com.sig.webservice.WebSecurityGBD.doInBackground(WebSec urityGBD.java:23)
at android.os.AsyncTask $ 2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)
在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
在java.util.concurrent .ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
引起:java.lang.IllegalStateException:期望的BEGIN_ARRAY但是BEGIN_OBJECT在第1行第60列路径$ .HabilitationAD [0]
在com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350)
在com.google.gson.internal.bind。 MapTypeAdapterFactory $ Adapter.read(MapTypeAdapterFactory.java:172)
at com.google.gson.internal.bind.MapTypeAdapterFactory $ Adapter.read(MapTypeAdapterFactor y.java:145)
在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.read(ReflectiveTypeAdapterFactory.java:103)
在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $适配器.read(ReflectiveTypeAdapterFactory.java:196)
在com.google.gson.Gson.fromJson(Gson.java:810)
在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)
在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java :1112)
在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587)$ b $在java.lang.Thread.run(Thread.java:818)
编辑2
来自两个HashMap>
@SerializedName(HabilitationAD)
私有地图< String,List< HabilitationBean> > mHabilitationAD = new HashMap< String,List< HabilitationBean>>();
@SerializedName(HabilitationInterprete)
私人地图< String,List< HabilitationBean>> mHabilitationInterprete = new HashMap< String,List< HabilitationBean>>();
如果我评论这个属性,我没有任何问题来构建我的json objet。
我发现此链接反序列化a地图< String,Object>领域与Gson
目前我尝试这样的事情
public class InfoSecuriteBeanDeserializer实现JsonDeserializer< InfoSecuriteBean> {
@Override
public InfoSecuriteBean deserialize(JsonElement json,Type type,JsonDeserializationContext jsonDeserializationContext)throws JsonParseException {
JsonArray jArray =(JsonArray)json;
InfoSecuriteBean mInfoSecuriteBean = new InfoSecuriteBean(); (i = 1; i< jArray.size(); i ++){
JsonObject jObject =(JsonObject)jArray.get(i);
//假设您有合适的构造函数...
HabilitationBean mHabilitationBean = new HabilitationBean(jObject.get(Applicatif)。getAsString(),
jObject.get(Cle) .getAsString(),
jObject.get(Role)。getAsString(),
jObject.get(Valeur)。getAsString());
mInfoSecuriteBean.getmHabilitationAD()。add(mHabilitationBean);
}
返回mInfoSecuriteBean;
$ / code $ / pre>
你认为我有很好的方式解决我的问题?
解决方案我找到了解决我的问题的简单解决方案。我会在这里展示我的解决方案,也许这会帮助一些人。
所以我创建了两个新类: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;
$ / code $ / pre
$ b $ p
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;
$ b $ public HabilitationInterprete(){
}
public HabilitationInterprete(String p_mKey,List< BeanHabilitation> p_mValue){
this.mKey = p_mKey;
this.mValue = p_mValue;
}
我修改我的类 InfoSecuriteBean $ c
$ p我改变了这个:
@SerializedName( HabilitationAD)
私人地图< String,List< HabilitationBean>> mHabilitationAD = new HashMap< String,List< HabilitationBean>>();
@SerializedName(HabilitationInterprete)
私人地图< String,List< HabilitationBean>> mHabilitationInterprete = new HashMap< String,List< HabilitationBean>>();
by this:
@SerializedName(HabilitationAD)
私人列表< HabilitationAD> habilitationAD;
@SerializedName(HabilitationInterprete)
私人列表< HabilitationInterprete> habilitationInterprete;
现在所有的工作都很棒!希望这个解决方案能帮到你!
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 ?
解决方案 I found a simply solution for solve my problem. I will show here my solution, maybe this will help some people.
So I created two new class : HabilitationAD and 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 I modify my class InfoSecuriteBean
I changed this :
@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 this :
@SerializedName("HabilitationAD")
private List<HabilitationAD> habilitationAD;
@SerializedName("HabilitationInterprete")
private List<HabilitationInterprete> habilitationInterprete;
Now all work great ! Hope this solution will help !
这篇关于GSON预计BEGIN_ARRAY,但是BEGIN_OBJECT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!