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

标签 java json jackson

我的 json API 返回多个网络值,我的网站仅显示第一个值,当我的 pojo 重置即将到来的值并将其自身设置为收到的最后一个值时,我尝试以这样的方式更改我的 pojo 文件:值已设置为拒绝或不应接受另一对。我是 json 和 Jackson 的新手,所以如果我对您的答案提出更多问题,请放轻松,并且我正在尝试将 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");
     }
}

主要 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);
}

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

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

关于java - Json 文件返回多个值,我希望我的 getter 和 setter 在设置为第一个值后拒绝其他值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28116878/

相关文章:

javascript - 将对象转换为 JSON 字符串并将其置空

java - org.codehaus.jackson.JsonParseException : Unexpected character ('' (code 65279/0xfeff)): expected a valid value

java - @class 被添加,即使它是 defaultImpl

Java http 客户端和 POODLE

java - 单击按钮后更新按钮文本

android - 如何在Android上解析 "unit-test"json(JSONObject == "null")?

json - 如何使用 ConvertTo-Json 将 powershell 数组转换为 json?

java - 将 JSON 映射到 POJO 时出现 UnrecognizedPropertyException

java - 使用 IN 运算符在多对多关系中进行 JPA 条件查询

java - 此 OutputStream 代码不会将字符串保存到 Txt 文件中