使用gson加载JSON

问题描述 投票:0回答:1

[尝试加载JSON文件,出现错误,但这是什么意思?

   public class MovieCollection{
        private List<Movie> movies;
        private Movie movie;

        public MovieCollection() {

    this.movies = new ArrayList<Movie>();
} 

/**
 * Creates a new book collection with the specified list of books pre-defined.
 *
 * @param books A books list.
 */
public MovieCollection(List<Movie> movies) {
    this.movies= movies;
}

            public static MovieCollection loadFromJSONFile (File file){
                Gson gson = new Gson();
                JsonReader jsonReader = null;
                try {
                    jsonReader = new JsonReader(new FileReader(file));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    System.out.println("Error");
                }
                return gson.fromJson(jsonReader,  MovieCollection.class);
            }

预期是BEGIN_OBJECT,但在第1行第2列路径$处为BEGIN_ARRAY,但>

[尝试加载JSON文件,出现错误,但这是什么意思?公共类MovieCollection {私有列表电影;私人电影电影;公共...

json gson
1个回答
1
投票

错误清楚地表明您尝试读取的文本实际上是一个数组,而不是JSON。它是JSON数组。这意味着您需要某种读取对象数组的方式,如下所示:

© www.soinside.com 2019 - 2024. All rights reserved.