我试图以下列形式解析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)
答案 0 :(得分:6)
JSON的根对象是一个对象,而不是一个数组。
这里需要两个类,一个用于表示外部对象,另一个用于表示电影本身:
MovieList
课程重命名为Movie
Movie
类,如下所示:MovieList:
public class MovieList {
@SerializedName("movie_list")
public List<Movie> Movies;
}