Json 文件返回多个值,我希望我的 getter 和 setter 在设置为第一个值后拒绝其他值



我的 json API 返回多个网络值,我的网站仅显示第一个值,当我的 pojo 重置即将到来的值并将自己设置为收到的最后一个值时,我正在尝试更改我的 pojo 文件,如果值已经设置,它会拒绝或不应该接受另一对。我是 json 和杰克逊的新手,所以如果我问更多关于您的答案的问题,并且我正在尝试将 json 的数据与实时站点的数据进行比较,请放轻松。

我已经尝试过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");
     }
}

主要波乔类 -

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

它添加所有列表,子列表为您提供第一个值。

感谢您投票否决这个问题,这确实帮助了我的士气。

最新更新