Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : Error in parsing JSON array using GSON library [duplicate]

Possible Duplicate:
GSON throwing “Expected BEGIN_OBJECT but was BEGIN_ARRAY”?

Scenario : I am parsing an API which is is JSON format using GSON library. Here the JSON file I am parsing :

{
  "questions": [
    {
      "question_1": "Question 1",
      "options": [
        "option_1 : option1",
        "option_2 : option2",
        "option_3 : option 3",
        "option_4 : option 4"
      ]
    },
    {
      "question_2": "Question 2",
      "options": [
        "option_1 : option 1",
        "option_2 : option 2",
        "option_3 : option 3"
      ]
    }
  ]
}

And my object class looks like this :

public class Practise_Question_Object {

    public List<Questions> questions;

    public class Questions {

        @SerializedName("question")
        public String Question;

        public List<Options> options;

        public class Options {

            public String Option_1;

            public String Option_2;

            public String Option_3;

            public String Option_4;
        }

    }

Problem My problem is that whenever I try to parse values It show me error java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 53

if I chagne "options" from Array to Object then I can easily parse. But in my project (From PHP side) i cant use Object.

like image 456
Vipul Purohit Avatar asked Apr 27 '26 23:04

Vipul Purohit


1 Answers

What if you change this line:

public List<Options> options;

to:

public Options[] options;

It looks like your JSON contains an array of Options objects.


Otherwise can I suggest posting the actual code you use in the web service, just to confirm that you are using the GSOM DOM-style automatic parsing.

like image 152
Richard Le Mesurier Avatar answered Apr 30 '26 14:04

Richard Le Mesurier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!