Huy Huy - 1 year ago 134
Android Question

Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

I am trying to read and parse a JSON string which starts as an array (e.g.

[{test: "test"}]
) and I keep running into the error:

Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

The error in my log points to this line:

Gson gson = new GsonBuilder().create();
PayoutCharges payoutList = gson.fromJson(reader, PayoutCharges.class);

Following some stackoverflow answers, I created the PayoutCharges class as an array list of PayoutCharge. How do I fix this issue so that GSON knows that the JSON string is inside an array?


import java.util.ArrayList;


public class PayoutCharges {
//handle error
private Error mError;

public Error getError() {
return mError;

public void setError(Error error) {
mError = error;

//handle data
private ArrayList<PayoutCharge> mPayoutCharges;

public ArrayList<PayoutCharge> getPayoutCharges() {
return mPayoutCharges;

public void setPayoutCharges(ArrayList<PayoutCharge> payoutCharges) {
mPayoutCharges = payoutCharges;

After reading @Ridcully's response, I want to ask if there is a way for me to update
so that it knows that the JSON is an array. Something like

Answer Source

Below code works for your sample json value:

String val1 = "[{test: \"test\"}]";

final GsonBuilder gsonBuilder = new GsonBuilder();
final Gson gson = gsonBuilder.create();

TestCase[] testCase = gson.fromJson(val1, TestCase[].class);

The TestCase holder class:

private static class TestCase {
    private String field;

The test example you've shared has an array which has objects. So you have to use an array of your pojo class while deserializing the json value to an object(array).

If this answer does not help you (which means you have something different on your real json value), you should better share the real json that you are working on.