我試圖讀取並解析其開始作爲一個數組JSON字符串(如[{test: "test"}]
)和我一直運行到錯誤:
Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
的錯誤在我的日誌指向該行:
Gson gson = new GsonBuilder().create();
PayoutCharges payoutList = gson.fromJson(reader, PayoutCharges.class);
經過一些計算器的答案,我創建了PayoutCharges類作爲PayoutCharge數組列表。 如何解決此問題,以便GSON知道JSON字符串在數組內?
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;
}
}
閱讀@ Ridcully的響應後,我想問問,如果有對我更新PayoutCharges.java
,這樣它知道JSON是一個數組的方式。像@SerializedName([])
?
Tuncer你救了我的一天。感謝您的解決方案 – PPD 2015-03-14 07:06:57
簡單但很好的例子,這解決了我的問題!,謝謝 – RoDo 2016-02-17 12:30:49
謝謝你的工作 – 2017-09-26 11:56:32