0

我想知道如何格式化对象的 Gson 数组列表

我有以下类疾病

public class Disease {

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

带有变量的 setter 和 getter。

我在 android 布局中有一个微调器,我想用疾病名称填充。

下面是微调器代码

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

现在疾病名称将从 Web 服务发送,其响应将采用 Gson 格式,如下所示

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

我尝试使用下面的代码

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

但我收到以下错误

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)

错误在这一行

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

问题解决了 :Disease[] dis = gson.fromJson(json, Disease[].class);

我很聪明地认识到我应该得到一系列疾病而不是疾病对象的响应=)

4

2 回答 2

1

试试这个,你需要将你的 Json arraylist 转换为 Java ArrayList

Gson gson = new Gson();
ArrayList<Disease> disArray = gson.fromJson(json, new TypeToken<List<Disease>>(){}.getType());
于 2014-02-18T09:43:27.413 回答
0

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

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

于 2012-08-12T09:44:51.380 回答