Android JSon error "Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2"

25,028

Solution 1

The error explains whats wrong... u r returning an array and not a JSon object

try as following:

JSONArray ja = new JSONArray(jsonStringReturnedByService);

Data sections = new Data();

for (int i = 0; i < ja.length(); i++) {
    Section s = new Section();
    JSONObject jsonSection = ja.getJSONObject(i);

    s.SectionId = Integer.ValueOf(jsonSection.getString("SectionId"));
    s.SectionName = jsonSection.getString("SectionName");

   //add it to sections list
   sections.add(s);
}

return sections;

Solution 2

You're trying to create an non-Array(Collection) object from a JSONArray. The error is pretty clear: GSON was expecting the beginning of an object but found the beginning of an array instead.

Take a look at the documentation page below to see how to work with Array and Collection types with GSON

https://sites.google.com/site/gson/gson-user-guide#TOC-Collections-Examples

From the docs:

Array Examples

Gson gson = new Gson(); int[] ints = {1, 2, 3, 4, 5}; String[] strings = {"abc", "def", "ghi"};

(Serialization) gson.toJson(ints); ==> prints [1,2,3,4,5] gson.toJson(strings); ==> prints ["abc", "def", "ghi"]

(Deserialization) int[] ints2 = gson.fromJson("[1,2,3,4,5]", int[].class); ==> ints2 will be same as ints

We also support multi-dimensional arrays, with arbitrarily complex element types Collections Examples

Gson gson = new Gson(); Collection ints = Lists.immutableList(1,2,3,4,5);

(Serialization) String json = gson.toJson(ints); ==> json is [1,2,3,4,5]

(Deserialization) Type collectionType = new TypeToken>(){}.getType(); Collection ints2 = gson.fromJson(json, collectionType); ints2 is same as ints

Fairly hideous: note how we define the type of collection Unfortunately, no way to get around this in Java

Collections Limitations

Can serialize collection of arbitrary objects but can not deserialize from it Because there is no way for the user to indicate the type of the resulting object While deserializing, Collection must be of a specific generic type All of this makes sense, and is rarely a problem w> hen following good Java coding practices

Share:
25,028
Yaqub Ahmad
Author by

Yaqub Ahmad

My top Answer1, Answer2 on stackoverflow. My top question1, question2 on stackoverflow.

Updated on March 03, 2020

Comments

  • Yaqub Ahmad
    Yaqub Ahmad over 4 years

    I am getting JSon data from a web service, the sample data is given below:

    [
      {
        "SectionId": 1,
        "SectionName": "Android"
      }
    ]
    

    When i try to convert it, it throws an error, i am doing it as:

    Data data = new Gson().fromJson(jsonDataFromWebService, Data.class);
    

    My Section Class is:

    class Section
    {
        public int SectionId;
        public String SectionName;
    }
    
    class Data {
        public List<Section> sections;
    }
    

    The LogCat says:

    com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2

  • Prem
    Prem about 10 years
    This is a bad answer. Changing the way the API returns data is not always an option.
  • Badacadabra
    Badacadabra about 7 years
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.