Sto cercando di leggere e analizzare una stringa JSON che inizia come un array (ad esempio [{test: "test"}]
) e continuo a correre in errore:
Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
Il errore nei miei punti di registro a questa linea:
Gson gson = new GsonBuilder().create();
PayoutCharges payoutList = gson.fromJson(reader, PayoutCharges.class);
Dopo alcune risposte StackOverflow, ho creato la classe PayoutCharges come una lista di array di PayoutCharge. Come posso risolvere questo problema in modo che GSON sappia che la stringa JSON si trova all'interno di un array?
PayoutCharges.java
package com.app.driver.entity;
import java.util.ArrayList;
import com.google.gson.annotations.SerializedName;
public class PayoutCharges {
//handle error
@SerializedName("error")
private Error mError;
public Error getError() {
return mError;
}
public void setError(Error error) {
mError = error;
}
//handle data
@SerializedName("payoutCharges")
private ArrayList<PayoutCharge> mPayoutCharges;
public ArrayList<PayoutCharge> getPayoutCharges() {
return mPayoutCharges;
}
public void setPayoutCharges(ArrayList<PayoutCharge> payoutCharges) {
mPayoutCharges = payoutCharges;
}
}
Dopo aver letto la risposta di @ Ridcully, voglio chiedere se c'è un modo per me di aggiornare PayoutCharges.java
in modo che sappia che il JSON è un array. Qualcosa come @SerializedName([])
?
Tuncer hai salvato la mia giornata. Grazie per la soluzione – PPD
Semplice ma ottimo esempio, questo ha risolto il mio problema !, Grazie – RoDo
Grazie al fatto che funziona –