将JsonArray解析为自定义数组

时间:2018-03-29 00:43:39

标签: android json gson

我正在尝试使用gson将jsonarray解析为自定义数组。这就是我到现在为止所做的事情

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());
     }
}

JSONArray有数据,但是当我尝试创建movieList时,由于列表中的对象是空的,因此无效。

enter image description here

我不太了解从Json解析。有谁知道如何解决它?

这是我的电影课:

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;
}

这是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"}

1 个答案:

答案 0 :(得分:1)

你有一个电影课。将“电影”添加到您的字段是多余的。

默认情况下,所有JSON密钥必须与Gson的Java字段完全匹配才能工作。

例如,movieOriginalTitle应该是original_title

总的来说,我建议您阅读Gson documentation