GSON无法反序列化数组

时间:2017-12-05 10:29:26

标签: java android json gson

我遇到了Gson的问题。我提取格式化的Json数据。这是语法:

I/Reponse-Json: [{"consultation_id":281,"plageconsult_id":76,"patient_id":116,"heure":"11:04:00","duree":1,"motif":null,"secteur1":0,"secteur2":0,"rques":null,"chrono":"32","annule":"0","patient_date_reglement":null,"examen":null,"traitement":null,"histoire_maladie":null,"conclusion":null,"premiere":"0","derniere":"0","tarif":null,"patient_mode_reglement":null,"arrivee":"2017-11-22 11:04:00","codes_ccam":null,"banque_id":null,"categorie_id":null,"adresse":"0","valide":"0","total_amc":null,"total_amo":null,"total_assure":null,"du_patient":0,"sejour_id":252,"du_tiers":0,"tiers_mode_reglement":null,"tiers_date_reglement":null,"date_at":null,"concerne_ALD":"0","facture":"0","adresse_par_prat_id":null,"si_desistement":"0","fin_at":null,"pec_at":null,"reprise_at":null,"type":"classique","grossesse_id":null,"at_sans_arret":"0","arret_maladie":"0","factureconsult_id":null,"brancardage":null,"sianesthesique":0},{"consultation_id":282,"plageconsult_id":77,"patient_id":116,"heure":"16:31:00","duree":1,"motif":null,"secteur1":0,"secteur2":0,"rques":"RAS","chrono":"64","annule":"0","patient_date_reglement":null,"examen":null,"traitement":"HUILE DE PARAFINE FL DE 1 LITRE\n","histoire_maladie":null,"conclusion":null,"premiere":"0","derniere":"0","tarif":null,"patient_mode_reglement":null,"arrivee":"2017-11-22 16:31:00","codes_ccam":null,"banque_id":null,"categorie_id":null,"adresse":"0","valide":"0","total_amc":null,"total_amo":null,"total_assure":null,"du_patient":0,"sejour_id":252,"du_tiers":0,"tiers_mode_reglement":null,"tiers_date_reglement":null,"date_at":null,"concerne_ALD":"0","facture":"0","adresse_par_prat_id":null,"si_desistement":"0","fin_at":null,"pec_at":null,"reprise_at":null,"type":"classique","grossesse_id":null,"at_sans_arret":"0","arret_maladie":"0","factureconsult_id":null,"brancardage":null,"sianesthesique":0}]

以下是用于反序列化的代码

    Log.i("Reponse-Json", response);
    List<Consultation> consultations = Arrays.asList(gson.fromJson(response, Consultation[].class));
    Log.i("ConsultActivity", consultations.size()+" consultations loaded");
    for (Consultation c : consultations) {
        Log.i("ConsultActivity", ""+c.getConsultationId());
    }

这是错误:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 72
                                                                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
                                                                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
                                                                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
                                                                  at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
                                                                  at com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:72)
                                                                  at com.google.gson.Gson.fromJson(Gson.java:803)
                                                                  at com.google.gson.Gson.fromJson(Gson.java:768)
                                                                  at com.google.gson.Gson.fromJson(Gson.java:717)
                                                                  at com.google.gson.Gson.fromJson(Gson.java:689)
                                                                  at org.cfrtmbk.www.elaafi.ConsultActivity$1.onResponse(ConsultActivity.java:69)
                                                                  at org.cfrtmbk.www.elaafi.ConsultActivity$1.onResponse(ConsultActivity.java:65)
                                                                  at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
                                                                  at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
                                                                  at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
                                                                  at android.os.Handler.handleCallback(Handler.java:751)
                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                  at android.os.Looper.loop(Looper.java:154)
                                                                  at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                               Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 72
                                                                  at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:374)
                                                                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory

我尝试使用类似于以下的解决方案:

Type collectionType = new TypeToken<Collection<channelSearchEnum>>(){}.getType();
Collection<channelSearchEnum> enums = gson.fromJson(yourJson, collectionType);

但有同样的问题 请帮帮我

3 个答案:

答案 0 :(得分:1)

public static <T> List<T> toList(String json, Class<T> clazz) {
    if (null == json) {
        return null;
    }
    Gson gson = new Gson();
    return gson.fromJson(json, new TypeToken<T>(){}.getType());
}

示例电话:

List<Consultation > objects = GsonUtils.toList(response, Consultation .class);

答案 1 :(得分:0)

假设您的咨询课程是以这种方式编写的:

public class Consultation{
    private Integer id;
    private String adresse;
}

您可以通过以下方式提取您发布到广告ArrayList中的JSON:

ArrayList<Consultation> consultationList = gson.fromJson(json ,new TypeToken<List<Consultation>>(){}.getType());

答案 2 :(得分:0)

如果您正在使用gson库,那么试试这个

Gson gson = new Gson();
   List<Consultation> consultations= gson.fromJson(json, new TypeToken<List<Consultation>>(){}.getType());
    for (Consultation consultation: consultations) {
        Log.e("TEST","Consultation id : " + consultation.getConsulationId());
        Log.e("TEST","Patient id: " + consultation.getPatientid());
    }

Consultation.java

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Consultation{

@SerializedName("consultation_id")
@Expose
private Integer consultationId;
@SerializedName("plageconsult_id")
@Expose
private Integer plageconsultId;
@SerializedName("patient_id")
@Expose
private Integer patientId;
@SerializedName("heure")
@Expose
private String heure;
@SerializedName("duree")
@Expose
private Integer duree;
@SerializedName("motif")
@Expose
private Object motif;
@SerializedName("secteur1")
@Expose
private Integer secteur1;
@SerializedName("secteur2")
@Expose
private Integer secteur2;
@SerializedName("rques")
@Expose
private String rques;
@SerializedName("chrono")
@Expose
private String chrono;
@SerializedName("annule")
@Expose
private String annule;
@SerializedName("patient_date_reglement")
@Expose
private Object patientDateReglement;
@SerializedName("examen")
@Expose
private Object examen;
@SerializedName("traitement")
@Expose
private String traitement;
@SerializedName("histoire_maladie")
@Expose
private Object histoireMaladie;
@SerializedName("conclusion")
@Expose
private Object conclusion;
@SerializedName("premiere")
@Expose
private String premiere;
@SerializedName("derniere")
@Expose
private String derniere;
@SerializedName("tarif")
@Expose
private Object tarif;
@SerializedName("patient_mode_reglement")
@Expose
private Object patientModeReglement;
@SerializedName("arrivee")
@Expose
private String arrivee;
@SerializedName("codes_ccam")
@Expose
private Object codesCcam;
@SerializedName("banque_id")
@Expose
private Object banqueId;
@SerializedName("categorie_id")
@Expose
private Object categorieId;
@SerializedName("adresse")
@Expose
private String adresse;
@SerializedName("valide")
@Expose
private String valide;
@SerializedName("total_amc")
@Expose
private Object totalAmc;
@SerializedName("total_amo")
@Expose
private Object totalAmo;
@SerializedName("total_assure")
@Expose
private Object totalAssure;
@SerializedName("du_patient")
@Expose
private Integer duPatient;
@SerializedName("sejour_id")
@Expose
private Integer sejourId;
@SerializedName("du_tiers")
@Expose
private Integer duTiers;
@SerializedName("tiers_mode_reglement")
@Expose
private Object tiersModeReglement;
@SerializedName("tiers_date_reglement")
@Expose
private Object tiersDateReglement;
@SerializedName("date_at")
@Expose
private Object dateAt;
@SerializedName("concerne_ALD")
@Expose
private String concerneALD;
@SerializedName("facture")
@Expose
private String facture;
@SerializedName("adresse_par_prat_id")
@Expose
private Object adresseParPratId;
@SerializedName("si_desistement")
@Expose
private String siDesistement;
@SerializedName("fin_at")
@Expose
private Object finAt;
@SerializedName("pec_at")
@Expose
private Object pecAt;
@SerializedName("reprise_at")
@Expose
private Object repriseAt;
@SerializedName("type")
@Expose
private String type;
@SerializedName("grossesse_id")
@Expose
private Object grossesseId;
@SerializedName("at_sans_arret")
@Expose
private String atSansArret;
@SerializedName("arret_maladie")
@Expose
private String arretMaladie;
@SerializedName("factureconsult_id")
@Expose
private Object factureconsultId;
@SerializedName("brancardage")
@Expose
private Object brancardage;
@SerializedName("sianesthesique")
@Expose
private Integer sianesthesique;

public Integer getConsultationId() {
return consultationId;
}

public void setConsultationId(Integer consultationId) {
this.consultationId = consultationId;
}

public Integer getPlageconsultId() {
return plageconsultId;
}

public void setPlageconsultId(Integer plageconsultId) {
this.plageconsultId = plageconsultId;
}

public Integer getPatientId() {
return patientId;
}

public void setPatientId(Integer patientId) {
this.patientId = patientId;
}

public String getHeure() {
return heure;
}

public void setHeure(String heure) {
this.heure = heure;
}

public Integer getDuree() {
return duree;
}

public void setDuree(Integer duree) {
this.duree = duree;
}

public Object getMotif() {
return motif;
}

public void setMotif(Object motif) {
this.motif = motif;
}

public Integer getSecteur1() {
return secteur1;
}

public void setSecteur1(Integer secteur1) {
this.secteur1 = secteur1;
}

public Integer getSecteur2() {
return secteur2;
}

public void setSecteur2(Integer secteur2) {
this.secteur2 = secteur2;
}

public String getRques() {
return rques;
}

public void setRques(String rques) {
this.rques = rques;
}

public String getChrono() {
return chrono;
}

public void setChrono(String chrono) {
this.chrono = chrono;
}

public String getAnnule() {
return annule;
}

public void setAnnule(String annule) {
this.annule = annule;
}

public Object getPatientDateReglement() {
return patientDateReglement;
}

public void setPatientDateReglement(Object patientDateReglement) {
this.patientDateReglement = patientDateReglement;
}

public Object getExamen() {
return examen;
}

public void setExamen(Object examen) {
this.examen = examen;
}

public String getTraitement() {
return traitement;
}

public void setTraitement(String traitement) {
this.traitement = traitement;
}

public Object getHistoireMaladie() {
return histoireMaladie;
}

public void setHistoireMaladie(Object histoireMaladie) {
this.histoireMaladie = histoireMaladie;
}

public Object getConclusion() {
return conclusion;
}

public void setConclusion(Object conclusion) {
this.conclusion = conclusion;
}

public String getPremiere() {
return premiere;
}

public void setPremiere(String premiere) {
this.premiere = premiere;
}

public String getDerniere() {
return derniere;
}

public void setDerniere(String derniere) {
this.derniere = derniere;
}

public Object getTarif() {
return tarif;
}

public void setTarif(Object tarif) {
this.tarif = tarif;
}

public Object getPatientModeReglement() {
return patientModeReglement;
}

public void setPatientModeReglement(Object patientModeReglement) {
this.patientModeReglement = patientModeReglement;
}

public String getArrivee() {
return arrivee;
}

public void setArrivee(String arrivee) {
this.arrivee = arrivee;
}

public Object getCodesCcam() {
return codesCcam;
}

public void setCodesCcam(Object codesCcam) {
this.codesCcam = codesCcam;
}

public Object getBanqueId() {
return banqueId;
}

public void setBanqueId(Object banqueId) {
this.banqueId = banqueId;
}

public Object getCategorieId() {
return categorieId;
}

public void setCategorieId(Object categorieId) {
this.categorieId = categorieId;
}

public String getAdresse() {
return adresse;
}

public void setAdresse(String adresse) {
this.adresse = adresse;
}

public String getValide() {
return valide;
}

public void setValide(String valide) {
this.valide = valide;
}

public Object getTotalAmc() {
return totalAmc;
}

public void setTotalAmc(Object totalAmc) {
this.totalAmc = totalAmc;
}

public Object getTotalAmo() {
return totalAmo;
}

public void setTotalAmo(Object totalAmo) {
this.totalAmo = totalAmo;
}

public Object getTotalAssure() {
return totalAssure;
}

public void setTotalAssure(Object totalAssure) {
this.totalAssure = totalAssure;
}

public Integer getDuPatient() {
return duPatient;
}

public void setDuPatient(Integer duPatient) {
this.duPatient = duPatient;
}

public Integer getSejourId() {
return sejourId;
}

public void setSejourId(Integer sejourId) {
this.sejourId = sejourId;
}

public Integer getDuTiers() {
return duTiers;
}

public void setDuTiers(Integer duTiers) {
this.duTiers = duTiers;
}

public Object getTiersModeReglement() {
return tiersModeReglement;
}

public void setTiersModeReglement(Object tiersModeReglement) {
this.tiersModeReglement = tiersModeReglement;
}

public Object getTiersDateReglement() {
return tiersDateReglement;
}

public void setTiersDateReglement(Object tiersDateReglement) {
this.tiersDateReglement = tiersDateReglement;
}

public Object getDateAt() {
return dateAt;
}

public void setDateAt(Object dateAt) {
this.dateAt = dateAt;
}

public String getConcerneALD() {
return concerneALD;
}

public void setConcerneALD(String concerneALD) {
this.concerneALD = concerneALD;
}

public String getFacture() {
return facture;
}

public void setFacture(String facture) {
this.facture = facture;
}

public Object getAdresseParPratId() {
return adresseParPratId;
}

public void setAdresseParPratId(Object adresseParPratId) {
this.adresseParPratId = adresseParPratId;
}

public String getSiDesistement() {
return siDesistement;
}

public void setSiDesistement(String siDesistement) {
this.siDesistement = siDesistement;
}

public Object getFinAt() {
return finAt;
}

public void setFinAt(Object finAt) {
this.finAt = finAt;
}

public Object getPecAt() {
return pecAt;
}

public void setPecAt(Object pecAt) {
this.pecAt = pecAt;
}

public Object getRepriseAt() {
return repriseAt;
}

public void setRepriseAt(Object repriseAt) {
this.repriseAt = repriseAt;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public Object getGrossesseId() {
return grossesseId;
}

public void setGrossesseId(Object grossesseId) {
this.grossesseId = grossesseId;
}

public String getAtSansArret() {
return atSansArret;
}

public void setAtSansArret(String atSansArret) {
this.atSansArret = atSansArret;
}

public String getArretMaladie() {
return arretMaladie;
}

public void setArretMaladie(String arretMaladie) {
this.arretMaladie = arretMaladie;
}

public Object getFactureconsultId() {
return factureconsultId;
}

public void setFactureconsultId(Object factureconsultId) {
this.factureconsultId = factureconsultId;
}

public Object getBrancardage() {
return brancardage;
}

public void setBrancardage(Object brancardage) {
this.brancardage = brancardage;
}

public Integer getSianesthesique() {
return sianesthesique;
}

public void setSianesthesique(Integer sianesthesique) {
this.sianesthesique = sianesthesique;
}

}