引起:java.lang.IllegalStateException:预期BEGIN_ARRAY但在第1行第2列是BEGIN_OBJECT

时间:2015-01-19 20:54:29

标签: java android exception android-asynctask gson

我试图以下列形式解析gson响应:

{"movie_list":[
           {"movie_id":"1","movie_name":"Avatar 2009"},                
           {"movie_id":"2","movie_name":"Titanic"}
           ]}

使用GSON库,如下所示:

        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.create();
        movieList = new ArrayList<MovieList>();
        movieList = Arrays.asList(gson.fromJson(reader, MovieList[].class));

我制作了一个MovieList类:

public class MovieList {

@SerializedName("movie_id")
public String movieId;

@SerializedName("movie_name")
public String movieName;

public String getMovieId() {
    return movieId;
}

public void setMovieId(String movieId) {
    this.movieId = movieId;
}

public String getMovieName() {
    return movieName;
}

public void setMovieName(String movieName) {
    this.movieName = movieName;
}

}

但是得到这个例外:

    01-19 15:48:09.425: E/AndroidRuntime(4868): FATAL EXCEPTION: AsyncTask #1
01-19 15:48:09.425: E/AndroidRuntime(4868): Process: com.example.autocompletetextviewdemo, PID: 4868
01-19 15:48:09.425: E/AndroidRuntime(4868): java.lang.RuntimeException: An error occured while executing doInBackground()
01-19 15:48:09.425: E/AndroidRuntime(4868):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
01-19 15:48:09.425: E/AndroidRuntime(4868):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
01-19 15:48:09.425: E/AndroidRuntime(4868):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
01-19 15:48:09.425: E/AndroidRuntime(4868):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
01-19 15:48:09.425: E/AndroidRuntime(4868):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
01-19 15:48:09.425: E/AndroidRuntime(4868):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
01-19 15:48:09.425: E/AndroidRuntime(4868):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
01-19 15:48:09.425: E/AndroidRuntime(4868):     at java.lang.Thread.run(Thread.java:841)
01-19 15:48:09.425: E/AndroidRuntime(4868): Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2
01-19 15:48:09.425: E/AndroidRuntime(4868):     at com.google.gson.Gson.fromJson(Gson.java:815)
01-19 15:48:09.425: E/AndroidRuntime(4868):     at com.google.gson.Gson.fromJson(Gson.java:741)
01-19 15:48:09.425: E/AndroidRuntime(4868):     at com.example.autocompletetextviewdemo.MainActivity$AutoCompleteText.doInBackground(MainActivity.java:152)
01-19 15:48:09.425: E/AndroidRuntime(4868):     at com.example.autocompletetextviewdemo.MainActivity$AutoCompleteText.doInBackground(MainActivity.java:1)
01-19 15:48:09.425: E/AndroidRuntime(4868):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
01-19 15:48:09.425: E/AndroidRuntime(4868):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
01-19 15:48:09.425: E/AndroidRuntime(4868):     ... 4 more
01-19 15:48:09.425: E/AndroidRuntime(4868): Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2
01-19 15:48:09.425: E/AndroidRuntime(4868):     at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:338)
01-19 15:48:09.425: E/AndroidRuntime(4868):     at com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:70)
01-19 15:48:09.425: E/AndroidRuntime(4868):     at com.google.gson.Gson.fromJson(Gson.java:803)

1 个答案:

答案 0 :(得分:6)

JSON的根对象是一个对象,而不是一个数组。

这里需要两个类,一个用于表示外部对象,另一个用于表示电影本身:

  1. 将您的MovieList课程重命名为Movie
  2. 创建一个新的Movie类,如下所示:
  3. MovieList:

    public class MovieList {
        @SerializedName("movie_list")
        public List<Movie> Movies;
    }