0

I'm trying to parse an jsonarray to a custom array using gson. This is what I did until now

try {
        String jsonData = response.body().string();
        JSONObject mObject = new JSONObject(jsonData);

        JSONArray movies = mObject.getJSONArray("results");

        Type listType = new TypeToken<List<Movie>>(){}.getType();
        List<Movie> movieList = mGson.fromJson(movies.toString(), listType);

     } catch (IOException e) {
                Log.d(TAG + " Failure 1", e.getMessage());
     } catch (JSONException e) {
                Log.d(TAG + " Failure 2", e.getMessage());
     }
}

The JSONArray has data but when I try to create the movieList, doesn't work right because the objects inside the list are empty.

enter image description here

I don't know too much about parsing from Json. Does anyone know how to solve it?

This is my movie class:

public class Movie implements Serializable{

private String movieOriginalTitle;
private String moviePoster;
private String movieSynopsis;
private String movieRate;
private String movieRelaseDate;


public String getMovieOriginalTitle() {
    return movieOriginalTitle;
}

public void setMovieOriginalTitle(String movieOriginalTitle) {
    this.movieOriginalTitle = movieOriginalTitle;
}

public String getMoviePoster() {
    return moviePoster;
}

public void setMoviePoster(String moviePoster) {
    this.moviePoster = moviePoster;
}

public String getMovieSynopsis() {
    return movieSynopsis;
}

public void setMovieSynopsis(String movieSynopsis) {
    this.movieSynopsis = movieSynopsis;
}

public String getMovieRate() {
    return movieRate;
}

public void setMovieRate(String movieRate) {
    this.movieRate = movieRate;
}

public String getMovieRelaseDate() {
    return movieRelaseDate;
}

public void setMovieRelaseDate(String movieRelaseDate) {
    this.movieRelaseDate = movieRelaseDate;
}

And this is a value of the JSONArray

{"vote_count":1126,"id":337167,"video":false,"vote_average":6.1,"title":"Fifty Shades Freed","popularity":542.892558,"poster_path":"\/jjPJ4s3DWZZvI4vw8Xfi4Vqa1Q8.jpg","original_language":"en","original_title":"Fifty Shades Freed","genre_ids":[18,10749],"backdrop_path":"\/9ywA15OAiwjSTvg3cBs9B7kOCBF.jpg","adult":false,"overview":"Believing they have left behind shadowy figures from their past, newlyweds Christian and Ana fully embrace an inextricable connection and shared life of luxury. But just as she steps into her role as Mrs. Grey and he relaxes into an unfamiliar stability, new threats could jeopardize their happy ending before it even begins.","release_date":"2018-02-07"}

{"vote_count":6697,"id":269149,"video":false,"vote_average":7.7,"title":"Zootopia","popularity":340.221253,"poster_path":"\/sM33SANp9z6rXW8Itn7NnG1GOEs.jpg","original_language":"en","original_title":"Zootopia","genre_ids":[16,12,10751,35],"backdrop_path":"\/mhdeE1yShHTaDbJVdWyTlzFvNkr.jpg","adult":false,"overview":"Determined to prove herself, Officer Judy Hopps, the first bunny on Zootopia's police force, jumps at the chance to crack her first case - even if it means partnering with scam-artist fox Nick Wilde to solve the mystery.","release_date":"2016-02-11"}
5
  • What's the issue exactly? Gson automatically parsed it Commented Mar 29, 2018 at 0:45
  • Please add your JSON data and Movie class to the question Commented Mar 29, 2018 at 0:47
  • The json data too? Gson requires you add an empty constructor for your class, by the way Commented Mar 29, 2018 at 0:56
  • @cricket_007 I updated the info Commented Mar 29, 2018 at 0:58
  • Why don't you have every field of the JSON in your Movie class? Commented Mar 29, 2018 at 1:04

1 Answer 1

1

You have a Movie class. Adding "movie" to your fields is redundant.

And by default, all JSON keys must match exactly to the Java fields for Gson to work.

For example, movieOriginalTitle should instead be original_title

Overall, I recommend that you read the Gson documentation

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.