2015-07-10 117 views
0

我有一些值在我的對象是由值返回null從json轉換爲對象時,其他人不,我不明白爲什麼發生這種情況 這裏是我的代碼轉換null值從gson.fromJson返回

OriginalMovie originalMovie = gson.fromJson(jsonString, OriginalMovie.class); 

,這裏是我的JSON

{"page":1, 
"results":[{"adult":false, 
"backdrop_path":"/o4I5sHdjzs29hBWzHtS2MKD3JsM.jpg", 
"genre_ids":[878,28,53,12], 
"id":87101,"original_language":"en", 
"original_title":"Terminator Genisys", 
"overview":"The year is 2029. John Connor, leader of the resistance continues the war against the machines.", 
"release_date":"2015-07-01", 
"poster_path":"/5JU9ytZJyR3zmClGmVm9q4Geqbd.jpg", 
"popularity":54.970301, 
"title":"Terminator Genisys","video":false, 
"vote_average":6.4, 
"vote_count":197}], 
"total_pages":11666,"total_results":233312} 

,這裏是我的基類(包含結果)

package MovieReviewHelper; 
import java.util.ArrayList; 
import java.util.List; 
public class OriginalMovie 
{ 

    private long page; 
    private List<Result> results = new ArrayList<Result>(); 
    private long totalPages; 
    private long totalResults; 
    public long getPage() 
    { 
     return page; 
    } 
    public void setPage(long page) 
    { 
     this.page = page; 
    } 
    public List<Result> getResults() 
    { 
     return results; 
    } 
    public void setResults(List<Result> results) 
    { 
     this.results = results; 
    } 
    public long getTotalPages() { 
     return totalPages; 
    } 
    public void setTotalPages(long totalPages) 
    { 
     this.totalPages = totalPages; 
    } 
    public long getTotalResults() 
    { 
     return totalResults; 
    } 
    public void setTotalResults(long totalResults) 
    { 
     this.totalResults = totalResults; 
    } 

} 

,這裏是我的其他類

package MovieReviewHelper; 

import java.util.ArrayList; 
import java.util.List; 
public class Result { 

    private boolean adult; 
    private String backdropPath; 
    private List<Long> genreIds = new ArrayList<Long>(); 
    private long id; 
    private String originalLanguage; 
    private String originalTitle; 
    private String overview; 
    private String releaseDate; 
    private String posterPath; 
    private double popularity; 
    private String title; 
    private boolean video; 
    private double voteAverage; 
    private long voteCount; 
    public boolean isAdult() 
    { 
     return adult; 
    } 
    public void setAdult(boolean adult) 
    { 
     this.adult = adult; 
    } 
    public String getBackdropPath() 
    { 
     return backdropPath; 
    } 
    public void setBackdropPath(String backdropPath) 
    { 
     this.backdropPath = backdropPath; 
    } 
    public List<Long> getGenreIds() 
    { 
     return genreIds; 
    } 
    public void setGenreIds(List<Long> genreIds) 
    { 
     this.genreIds = genreIds; 
    } 
    public long getId() 
    { 
     return id; 
    } 
    public void setId(long id) 
    { 
     this.id = id; 
    } 
    public String getOriginalLanguage() 
    { 
     return originalLanguage; 
    } 
    public void setOriginalLanguage(String originalLanguage) 
    { 
     this.originalLanguage = originalLanguage; 
    } 
    public String getOriginalTitle() 
    { 
     return originalTitle; 
    } 
    public void setOriginalTitle(String originalTitle) 
    { 
     this.originalTitle = originalTitle; 
    } 
    public String getOverview() 
    { 
     return overview; 
    } 
    public void setOverview(String overview) 
    { 
     this.overview = overview; 
    } 
    public String getReleaseDate() 
    { 
     return releaseDate; 
    } 
    public void setReleaseDate(String releaseDate) 
    { 
     this.releaseDate = releaseDate; 
    } 
    public String getPosterPath() 
    { 
     return posterPath; 
    } 
    public void setPosterPath(String posterPath) 
    { 
     this.posterPath = posterPath; 
    } 
    public double getPopularity() 
    { 
     return popularity; 
    } 
    public void setPopularity(double popularity) 
    { 
     this.popularity = popularity; 
    } 
    public String getTitle() 
    { 
     return title; 
    } 
    public void setTitle(String title) 
    { 
     this.title = title; 
    } 
    public boolean isVideo() 
    { 
     return video; 
    } 
    public void setVideo(boolean video) 
    { 
     this.video = video; 
    } 
    public double getVoteAverage() 
    { 
     return voteAverage; 
    } 
    public void setVoteAverage(double voteAverage) 
    { 
     this.voteAverage = voteAverage; 
    } 
    public long getVoteCount() 
    { 
     return voteCount; 
    } 
    public void setVoteCount(long voteCount) 
    { 
     this.voteCount = voteCount; 
    } 


} 
+0

哪些返回爲null? – Sandeep

+0

除4個值(流行,標題,ID,概述) –

+0

以外的所有名稱都必須帶有下劃線? – Sandeep

回答

1

您的JSON和類變量應該具有相同的名稱。 backdrop_path in Json and backdropPath in class will not work