Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8372955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:33:41+00:00 2026-06-09T14:33:41+00:00

I want to know how to deformat Gson arraylist of an object I have

  • 0

I want to know how to deformat Gson arraylist of an object

I have the following class Disease

public class Disease {

    private int diseaseID;
    private String diseaseName;
    private String diseaseDescription;
    private String diseaseImageURL;
}

with setters and getters of the variables.

I have a spinner in android layout that I would like to fill with diseases names.

below is the spinner code

<Spinner
       android:id="@+id/consultation_deseases"
       android:layout_width="@dimen/bigSpinnerWidth"
       android:layout_height="@dimen/bigSpinnerHeight"
       android:prompt="@string/disease_prompt" />

now the diseases names will be sent from web service whose response will be in Gson format like this

[{"diseaseName":"Obesity","diseaseID":0},{"diseaseName":"Influenza","diseaseID":1},{"diseaseName":"Smoking","diseaseID":2},{"diseaseName":"FSD","diseaseID":3},{"diseaseName":"STI/STD","diseaseID":4}]

I tried to use the below code

Disease dis = gson.fromJson(json, Disease.class); 

but I got the following error

08-12 11:29:18.273: E/AndroidRuntime(7518): FATAL EXCEPTION: main
08-12 11:29:18.273: E/AndroidRuntime(7518): com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2
08-12 11:29:18.273: E/AndroidRuntime(7518):     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:180)
08-12 11:29:18.273: E/AndroidRuntime(7518):     at com.google.gson.Gson.fromJson(Gson.java:755)
08-12 11:29:18.273: E/AndroidRuntime(7518):     at com.google.gson.Gson.fromJson(Gson.java:721)
08-12 11:29:18.273: E/AndroidRuntime(7518):     at com.google.gson.Gson.fromJson(Gson.java:670)
08-12 11:29:18.273: E/AndroidRuntime(7518):     at com.google.gson.Gson.fromJson(Gson.java:642)
08-12 11:29:18.273: E/AndroidRuntime(7518):     at com.example.onlineconsultation.LoginActivity$3.onClick(LoginActivity.java:117)
08-12 11:29:18.273: E/AndroidRuntime(7518):     at android.view.View.performClick(View.java:3558)
08-12 11:29:18.273: E/AndroidRuntime(7518):     at android.view.View$PerformClick.run(View.java:14152)
08-12 11:29:18.273: E/AndroidRuntime(7518):     at android.os.Handler.handleCallback(Handler.java:605)
08-12 11:29:18.273: E/AndroidRuntime(7518):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-12 11:29:18.273: E/AndroidRuntime(7518):     at android.os.Looper.loop(Looper.java:137)
08-12 11:29:18.273: E/AndroidRuntime(7518):     at android.app.ActivityThread.main(ActivityThread.java:4514)
08-12 11:29:18.273: E/AndroidRuntime(7518):     at java.lang.reflect.Method.invokeNative(Native Method)
08-12 11:29:18.273: E/AndroidRuntime(7518):     at java.lang.reflect.Method.invoke(Method.java:511)
08-12 11:29:18.273: E/AndroidRuntime(7518):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
08-12 11:29:18.273: E/AndroidRuntime(7518):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
08-12 11:29:18.273: E/AndroidRuntime(7518):     at dalvik.system.NativeStart.main(Native Method)
08-12 11:29:18.273: E/AndroidRuntime(7518): Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2
08-12 11:29:18.273: E/AndroidRuntime(7518):     at com.google.gson.stream.JsonReader.expect(JsonReader.java:339)
08-12 11:29:18.273: E/AndroidRuntime(7518):     at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:322)
08-12 11:29:18.273: E/AndroidRuntime(7518):     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:168)

the error is in this line

Disease dis = gson.fromJson(json, Disease.class); 

PROBLEM SOLVED : Disease[] dis = gson.fromJson(json, Disease[].class);

I was smart enough to recognize I should get the response in an array of Disease instead of Disease object =)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:33:42+00:00Added an answer on June 9, 2026 at 2:33 pm

    USE : Disease[] dis = gson.fromJson(json, Disease[].class);

    instead of Disease dis = gson.fromJson(json, Disease.class);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want know, what regular expression should I have for my string. My string
I have a function like the following: Public Function testFunction(ByVal input_string As String) As
Hi friends i want know sectionIndexTitlesForTableView Action is works i have the following code...
I have an android app it displays web view.I want know the app idle
want to know why String behaves like value type while using ==. String s1
Want to know ways to store OBJECTS of a Class in some persistent storage.
I have a field with a datepicker. I want know if it is open.
I want to know what a virtual base class is and what it means.
I have video view it playing videos correctly but i want know video starting
I have mtiks a mobile analytic software in my application and I want know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.