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 =)
USE :
Disease[] dis = gson.fromJson(json, Disease[].class);instead of
Disease dis = gson.fromJson(json, Disease.class);