簡體   English   中英

Json文件返回多個值,我希望我的getter和setter在將其設置為第一個值后拒絕其他值

[英]Json file returns multiple values, I want my getter and setter to reject other value once it has been set for first value

我的json API返回了多個網絡值,我的網站僅顯示了第一個值,當我的pojo為即將到來的值重置並為接收到的最后一個值設置自身時,我正在嘗試以這樣的方式更改我的pojo文件:設置拒絕或不接受另一對。 我是json和jackson的新手,所以如果我想問更多有關您答案的問題,請放輕松,我正嘗試將json中的數據與實時網站中的數據進行比較。

我嘗試過this.name = name == null ? this.name : throw_(); this.name = name == null ? this.name : throw_(); 但這並不能解決問題。

pojo的示例-

@JsonIgnoreProperties(ignoreUnknown = true)
public class Networks{
    private String banner;
    private String description;
    private boolean is_locked;
    private String logo;
    private String name;
    private String network_analytics;
    private Number network_id;
    private String slug;
    private String thumbnail_url;
    private String url;

    public String getBanner(){
        return this.banner;
    }
    public void setBanner(String banner){
        this.banner = banner;
    }
    public String getDescription(){
        return this.description;
    }
    public void setDescription(String description){
        this.description = description;
    }
    public boolean getIs_locked(){
        return this.is_locked;
    }
    public void setIs_locked(boolean is_locked){
        this.is_locked = is_locked;
    }
    public String getLogo(){
        return this.logo;
    }
    public void setLogo(String logo){
        this.logo = logo;
    }
    public String getName(){
        return this.name;
    }
    public void setName(String name){
        this.name = name == null ? this.name : throw_();
    }
    public String getNetwork_analytics(){
        return this.network_analytics;
    }
    public void setNetwork_analytics(String network_analytics){
        this.network_analytics = network_analytics;
    }
    public Number getNetwork_id(){
        return this.network_id;
    }
    public void setNetwork_id(Number network_id){
        this.network_id = network_id;
    }
    public String getSlug(){
        return this.slug;
    }
    public void setSlug(String slug){
        this.slug = slug;
    }
    public String getThumbnail_url(){
        return this.thumbnail_url;
    }
    public void setThumbnail_url(String thumbnail_url){
        this.thumbnail_url = thumbnail_url;
    }
    public String getUrl(){
        return this.url;
    }
    public void setUrl(String url){
        this.url = url;
    }
     public String throw_() {
         throw new RuntimeException("Network name is already set, second network not allowed");
     }
}

Pojo主班-

@JsonIgnoreProperties(ignoreUnknown = true)
public class JsonGen{
    private String _type;
    private List cast;
    private Common_sense_data common_sense_data;
    private String common_sense_id;
    private List crew;
    private String description;
    private Number franchise_id;
    private List genres;
    private String guid;
    @JsonProperty("image")
    private Images images;
    private boolean is_locked;
    private boolean is_mobile;
    private boolean is_parental_locked;
    private String kind;
    private List<String> mobile_networks;
    private String most_recent_full_episode_added_date;
    private String name;
    private List<Networks> networks;
    private List<String> platforms;
    private List ratings;
    private String release_date;
    private List season_filters;
    private String slug;
    private String tms_id;

    public JsonGen(){
        System.out.println("in JsonGen");
        networks = new ArrayList<Networks>(); 

    }

    public String get_type(){
        return this._type;
    }
    public void set_type(String _type){
        this._type = _type;
    }
    public List getCast(){
        return this.cast;
    }
    public void setCast(List cast){
        this.cast = cast;
    }
    public Common_sense_data getCommon_sense_data(){
        return this.common_sense_data;
    }
    public void setCommon_sense_data(Common_sense_data common_sense_data){
        this.common_sense_data = common_sense_data;
    }
    public String getCommon_sense_id(){
        return this.common_sense_id;
    }
    public void setCommon_sense_id(String common_sense_id){
        this.common_sense_id = common_sense_id;
    }
    public List getCrew(){
        return this.crew;
    }
    public void setCrew(List crew){
        this.crew = crew;
    }
    public String getDescription(){
        return this.description;
    }
    public void setDescription(String description){
        this.description = description;
    }
    public Number getFranchise_id(){
        return this.franchise_id;
    }
    public void setFranchise_id(Number franchise_id){
        this.franchise_id = franchise_id;
    }
    public List getGenres(){
        return this.genres;
    }
    public void setGenres(List genres){
        this.genres = genres;
    }
    public String getGuid(){
        return this.guid;
    }
    public void setGuid(String guid){
        this.guid = guid;
    }
    public Images getImages(){
        return this.images;
    }
    public void setImages(Images images){
        this.images = images;
    }
    public boolean getIs_locked(){
        return this.is_locked;
    }
    public void setIs_locked(boolean is_locked){
        this.is_locked = is_locked;
    }
    public boolean getIs_mobile(){
        return this.is_mobile;
    }
    public void setIs_mobile(boolean is_mobile){
        this.is_mobile = is_mobile;
    }
    public boolean getIs_parental_locked(){
        return this.is_parental_locked;
    }
    public void setIs_parental_locked(boolean is_parental_locked){
        this.is_parental_locked = is_parental_locked;
    }
    public String getKind(){
        return this.kind;
    }
    public void setKind(String kind){
        this.kind = kind;
    }
    public List<String> getMobile_networks(){
        return this.mobile_networks;
    }
    public void setMobile_networks(List<String> mobile_networks){
        this.mobile_networks = mobile_networks;
    }
    public String getMost_recent_full_episode_added_date(){
        return this.most_recent_full_episode_added_date;
    }
    public void setMost_recent_full_episode_added_date(String most_recent_full_episode_added_date){
        this.most_recent_full_episode_added_date = most_recent_full_episode_added_date;
    }
    public String getName(){
        return this.name;
    }
    public void setName(String name){
        this.name = name;
    }
    public List getNetworks(){
        return this.networks;
    }
    public void setNetworks(List networks){
        System.out.println("in Set NW"+networks);

            //System.out.println("in IF NW is null");
            this.networks.addAll(networks);
        //else {
            //System.out.println("in ELse NW is not null");
            //throw_();
        //}

        //this.networks = networks;
    }
    public List<String> getPlatforms(){
        return this.platforms;
    }
    public void setPlatforms(List<String> platforms){
        this.platforms = platforms;
    }
    public List getRatings(){
        return this.ratings;
    }
    public void setRatings(List ratings){
        this.ratings = ratings;
    }
    public String getRelease_date(){
        return this.release_date;
    }
    public void setRelease_date(String release_date){
        this.release_date = release_date;
    }
    public List getSeason_filters(){
        return this.season_filters;
    }
    public void setSeason_filters(List season_filters){
        this.season_filters = season_filters;
    }
    public String getSlug(){
        return this.slug;
    }
    public void setSlug(String slug){
        this.slug = slug;
    }
    public String getTms_id(){
        return this.tms_id;
    }
    public void setTms_id(String tms_id){
        this.tms_id = tms_id;
    }

     public String throw_() {
         System.out.println("Network name is already set, second network not allowed");
         throw new RuntimeException("Network name is already set, second network not allowed");
     }
}

我已將網絡列表的JsonGen代碼更改為-

public void setNetworks(List networks) {
    Networks.add(networks);
    this.networks = networks.subList(0, 1);
}

它會添加所有列表,子列表會為您提供第一個值。

感謝您對這個問題投反對票,這確實幫助了我的士氣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM