简体   繁体   中英

Trouble deserializing JSON into Java objects with GSON

I have been trying to learn GSON, but I am struggling with it. I am trying to deserialize a JSON file into Java objects, using GSON. I have read a million other questions on here, and for the life of me, I can't understand what I'm doing wrong.

Here is my JSON text:

{
 "movies": [
  {
   "name": "The Shawshank Redemption",
   "url": "https://parsehub.com/sandbox/moviedetails?movie=The%20Shawshank%20Redemption",
   "IMAX": "06:00 PM",
   "rating": "9 . 2",
   "cast": [
    {
     "character": "Andy Dufresne",
     "actor": "Tim Robbins"
    },
    {
     "character": "Ellis Boyd 'Red' Redding",
     "actor": "Morgan Freeman"
    },
    {
     "character": "Warden Norton",
     "actor": "Bob Gunton"
    },
    {
     "character": "Heywood",
     "actor": "William Sadler"
    }
   ]
  },
  {
   "name": "Schindler's List",
   "url": "https://parsehub.com/sandbox/moviedetails?movie=Schindler%27s%20List",
   "IMAX": "06:15 PM",
   "rating": "8 . 9",
   "cast": [
    {
     "character": "Oskar Schindler",
     "actor": "Liam Neeson"
    },
    {
     "character": "Itzhak Stern",
     "actor": "Ben Kingsley"
    },
    {
     "character": "Amon Goeth",
     "actor": "Ralph Fiennes"
    }
   ]
  }
 ]
}

And here is my Java code:

import com.google.gson.Gson;
import java.io.*;
import java.lang.reflect.Type;
import java.util.ArrayList;

public class Main {

    public static void main(String[] args) throws FileNotFoundException {
        Gson gson = new Gson();
        Movies[] movies = gson.fromJson(new FileReader("src/main/input.json"), (Type) Movies.class);
        System.out.println(movies[0]);
    }

    class Movies {
        String name;
        String url;
        String IMAX;
        String rating;
        ArrayList<Cast> cast;
    }

    class Cast {
        ArrayList<CastMember> castMembers;
    }

    class CastMember{
        String character;
        String actor;
    }
}

When I run this, I get the following error:

Exception in thread "main" java.lang.ClassCastException: class com.Main$Movies cannot be cast to class [Lcom.Main$Movies; (com.Main$Movies and [Lcom.Main$Movies; are in unnamed module of loader 'app')
    at com.Main.main(Main.java:13)

Try using this

Gson gson = new Gson();
Movies[] movies = gson.fromJson(new JsonReader(new FileReader("src/main/input.json"))),Movies[].class);

or something like

Type listType = new TypeToken<List<Movies>>() {}.getType();
List<Movies> movies = new Gson().fromJson(new JsonReader(new FileReader("src/main/input.json"))), listType);

The JSON you are deserializing represents an object with a list of objects on it. The Java object you are trying to deserialize to needs to match that.

First, create a new class MovieList .

class MovieList {
     List<Movie> movies;
}

Update your Movies class to be called Movie , since it represents a single movie.

class Movie {
     String name;
     String url;
     String IMAX;
     String rating;
     List<Cast> cast;
}

Now try calling gson.fromJson(...) with the following

MovieList movieList = gson.fromJson(new FileReader("src/main/input.json"), MovieList.class);
System.out.println(movieList.getMovies().get(0));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM