1. 程式人生 > >Gson 解析時候報錯 Expected BEGIN OBJECT but was BEGIN ARRAY at li

Gson 解析時候報錯 Expected BEGIN OBJECT but was BEGIN ARRAY at li

                03-17 23:05:00.373 18335-18335/? E/AndroidRuntime: FATAL EXCEPTION: main03-17 23:05:00.373 18335-18335/? E/AndroidRuntime: Process: tech.androidstudio.jsongsondemo, PID: 1833503-17 23:05:00.373 18335-18335/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{tech.androidstudio.jsongsondemo/tech.androidstudio.jsongsondemo.MainActivity}: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2386)03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:148)03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5310)03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:  Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at com.google.gson.Gson.fromJson(Gson.java:879)03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at com.google.gson.Gson.fromJson(Gson.java:844)03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at com.google.gson.Gson.fromJson(Gson.java:793)03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at com.google.gson.Gson.fromJson(Gson.java:765)03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at tech.androidstudio.jsongsondemo.MainActivity.onCreate(MainActivity.java:54)03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5953)03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2386) 03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:148) 03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292) 03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135) 03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5310) 03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) 03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) 03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:  Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:388)03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:209)03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at com.google.gson.Gson.fromJson(Gson.java:879) 03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at com.google.gson.Gson.fromJson(Gson.java:844) 03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at com.google.gson.Gson.fromJson(Gson.java:793) 03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at com.google.gson.Gson.fromJson(Gson.java:765) 03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at tech.androidstudio.jsongsondemo.MainActivity.onCreate(MainActivity.java:54) 03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5953) 03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128) 03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267) 03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2386) 03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:148) 03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292) 03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135) 03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5310) 03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) 

03-17 23:05:00.373 18335-18335/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) 

原因是資料的前後多了[ ]中括號。

String gsonStr1= "[{name:'name0',age:0}]";

解決辦法:

將前後的中括號去掉就好了。

//修改後{name:'name0',age:0},要去掉前後的括號。String gsonStr1= "{name:'name0',age:0}";