当前位置: 首页 > 知识库问答 >
问题:

使用改型解析不一致的JSON到POJO

羊舌承天
2023-03-14
{
        "assetDetails": {
            "id": "0-0-175244",
            "rating": 5,
            "duration": 3538,
            "on_air": "true",
            "content_owner": "ghuiiio",
            "business_type": "premium_downloadable",
            "age_rating": "A",
            "title": "QWERTY",
            "actors": [
                "ABC",
                "PQR"
            ],
            "asset_type": 0,
            "original_title": "QWERTY",
            "directors": [
                "jgdhisl"
            ],
            "asset_subtype": "movie",
            "drm_key_id": "1cba26eb-9d76-4275-9a5c-c651640dc929",
            "subtitle_lang": [
                "en"
            ],
            "end_credits_start_s": "00:00:56",
            "orderid": 0,
            "extended": {
                "seo_title": "QWERTY",
                "broadcast_state": "",
                "music_directors": []
            },
            "season_details": "",
            "description": "XYZ",
            "genres": [
                {
                    "id": "Thriller",
                    "value": "Thriller"
                }
            ],
            "release_date": "2020-06-27T00:00:00",
            "subtitle_languages": [
                "en"
            ],
            "languages": [
                "hi"
            ],
            "audio_languages": [
                "hi"
            ]
    }
}
public class PlaybackDetails{
    @SerializedName("assetDetails")
    private AssetDetails assetDetails;

    @SerializedName("watchHistoryDetails")
    private WatchHistoryDetails watchHistoryDetails;

    @SerializedName("trailerDetails")
    private Object trailerDetails;

    @SerializedName("keyOsDetails")
    private KeyOsDetails keyOsDetails;

    @SerializedName("entitlement")
    private Entitlement entitlement;

    @SerializedName("adDetails")
    private AdDetails adDetails;

    @SerializedName("showDetails")
    private ShowDetails showDetails;

    
    public void setAssetDetails(AssetDetails assetDetails){
        this.assetDetails = assetDetails;
    }

    public AssetDetails getAssetDetails(){
        return assetDetails;
    }

    public void setWatchHistoryDetails(WatchHistoryDetails watchHistoryDetails){
        this.watchHistoryDetails = watchHistoryDetails;
    }

    public WatchHistoryDetails getWatchHistoryDetails(){
        return watchHistoryDetails;
    }

    public void setTrailerDetails(Object trailerDetails){
        this.trailerDetails = trailerDetails;
    }

    public Object getTrailerDetails(){
        return trailerDetails;
    }

    public void setKeyOsDetails(KeyOsDetails keyOsDetails){
        this.keyOsDetails = keyOsDetails;
    }

    public KeyOsDetails getKeyOsDetails(){
        return keyOsDetails;
    }

    public void setEntitlement(Entitlement entitlement){
        this.entitlement = entitlement;
    }

    public Entitlement getEntitlement(){
        return entitlement;
    }

    public void setAdDetails(AdDetails adDetails){
        this.adDetails = adDetails;
    }

    public AdDetails getAdDetails(){
        return adDetails;
    }

    public void setShowDetails(ShowDetails showDetails){
        this.showDetails = showDetails;
    }

    public ShowDetails getShowDetails(){
        return showDetails;
    }

    @Override
    public String toString(){
        return 
            "PlaybackDetails{" + 
            "convivaDetails = '" + convivaDetails + '\'' + 
            ",assetDetails = '" + assetDetails + '\'' + 
            ",watchHistoryDetails = '" + watchHistoryDetails + '\'' + 
            ",trailerDetails = '" + trailerDetails + '\'' + 
            ",keyOsDetails = '" + keyOsDetails + '\'' + 
            ",entitlement = '" + entitlement + '\'' + 
            ",adDetails = '" + adDetails + '\'' + 
            ",showDetails = '" + showDetails + '\'' + 
            "}";
        }
}

    public class AssetDetails{
    
        @SerializedName("asset_subtype")
        private String assetSubtype;
    
        @SerializedName("audio_languages")
        private List<String> audioLanguages;
    
        @SerializedName("subtitle_languages")
        private List<String> subtitleLanguages;
    
        @SerializedName("rating")
        private int rating;
    
        @SerializedName("description")
        private String description;
    
        @SerializedName("title")
        private String title;
    
        @SerializedName("drm_key_id")
        private String drmKeyId;
    
        @SerializedName("extended")
        private Extended extended;
    
        @SerializedName("duration")
        private int duration;
    
        @SerializedName("episode_number")
        private int episodeNumber;
    
        @SerializedName("video_url")
        private VideoUrl videoUrl;
    
        @SerializedName("subtitle_lang")
        private List<String> subtitleLang;
    
        @SerializedName("genres")
        private List<GenresItem> genres;
    
        @SerializedName("business_type")
        private String businessType;
    
        @SerializedName("asset_type")
        private int assetType;
    
        @SerializedName("tvshow")
        private String tvshow;
    
        @SerializedName("season")
        private String season;
    
        @SerializedName("id")
        private String id;
    
        @SerializedName("skip_available")
        private SkipAvailable skipAvailable;
    
        @SerializedName("cover_image")
        private String coverImage;
    
        @SerializedName("slug")
        private String slug;
    
        @SerializedName("season_details")
        private SeasonDetails seasonDetails;
    
        @SerializedName("original_title")
        private String originalTitle;
    
        @SerializedName("is_drm")
        private int isDrm;
    
        @SerializedName("languages")
        private List<String> languages;
    
        @SerializedName("list_image")
        private String listImage;
    
        @SerializedName("age_rating")
        private String ageRating;
    
        @SerializedName("orderid")
        private int orderid;
    
        @SerializedName("image_url")
        private String imageUrl;
    
        @SerializedName("end_credits_start_s")
        private String endCreditsStartS;
    
        @SerializedName("content_owner")
        private String contentOwner;
    
        @SerializedName("tags")
        private List<String> tags;
    
        @SerializedName("channels")
        private List<ChannelsItem> channels;
    
        @SerializedName("web_url")
        private String webUrl;
    
        @SerializedName("release_date")
        private String releaseDate;
    
        public void setAssetSubtype(String assetSubtype){
            this.assetSubtype = assetSubtype;
        }
    
        public String getAssetSubtype(){
            return assetSubtype;
        }
    
        public void setAudioLanguages(List<String> audioLanguages){
            this.audioLanguages = audioLanguages;
        }
    
        public List<String> getAudioLanguages(){
            return audioLanguages;
        }
    
        public void setSubtitleLanguages(List<String> subtitleLanguages){
            this.subtitleLanguages = subtitleLanguages;
        }
    
        public List<String> getSubtitleLanguages(){
            return subtitleLanguages;
        }
    
        public void setRating(int rating){
            this.rating = rating;
        }
    
        public int getRating(){
            return rating;
        }
    
        public void setDescription(String description){
            this.description = description;
        }
    
        public String getDescription(){
            return description;
        }
    
        public void setTitle(String title){
            this.title = title;
        }
    
        public String getTitle(){
            return title;
        }
    
        public void setDrmKeyId(String drmKeyId){
            this.drmKeyId = drmKeyId;
        }
    
        public String getDrmKeyId(){
            return drmKeyId;
        }
    
        public void setExtended(Extended extended){
            this.extended = extended;
        }
    
        public Extended getExtended(){
            return extended;
        }
    
        public void setDuration(int duration){
            this.duration = duration;
        }
    
        public int getDuration(){
            return duration;
        }
    
        public void setEpisodeNumber(int episodeNumber){
            this.episodeNumber = episodeNumber;
        }
    
        public int getEpisodeNumber(){
            return episodeNumber;
        }
    
        public void setVideoUrl(VideoUrl videoUrl){
            this.videoUrl = videoUrl;
        }
    
        public VideoUrl getVideoUrl(){
            return videoUrl;
        }
    
        public void setSubtitleLang(List<String> subtitleLang){
            this.subtitleLang = subtitleLang;
        }
    
        public List<String> getSubtitleLang(){
            return subtitleLang;
        }
    
        public void setGenres(List<GenresItem> genres){
            this.genres = genres;
        }
    
        public List<GenresItem> getGenres(){
            return genres;
        }
    
        public void setBusinessType(String businessType){
            this.businessType = businessType;
        }
    
        public String getBusinessType(){
            return businessType;
        }
    
        public void setAssetType(int assetType){
            this.assetType = assetType;
        }
    
        public int getAssetType(){
            return assetType;
        }
    
        public void setTvshow(String tvshow){
            this.tvshow = tvshow;
        }
    
        public String getTvshow(){
            return tvshow;
        }
    
        public void setSeason(String season){
            this.season = season;
        }
    
        public String getSeason(){
            return season;
        }
    
        public void setId(String id){
            this.id = id;
        }
    
        public String getId(){
            return id;
        }
    
        public void setSkipAvailable(SkipAvailable skipAvailable){
            this.skipAvailable = skipAvailable;
        }
    
        public SkipAvailable getSkipAvailable(){
            return skipAvailable;
        }
    
        public void setCoverImage(String coverImage){
            this.coverImage = coverImage;
        }
    
        public String getCoverImage(){
            return coverImage;
        }
    
        public void setSlug(String slug){
            this.slug = slug;
        }
    
        public String getSlug(){
            return slug;
        }
    
        public void setSeasonDetails(SeasonDetails seasonDetails){
            this.seasonDetails = seasonDetails;
        }
    
        public SeasonDetails getSeasonDetails(){
            return seasonDetails;
        }
    
        public void setOriginalTitle(String originalTitle){
            this.originalTitle = originalTitle;
        }
    
        public String getOriginalTitle(){
            return originalTitle;
        }
    
        public void setIsDrm(int isDrm){
            this.isDrm = isDrm;
        }
    
        public int getIsDrm(){
            return isDrm;
        }
    
        public void setLanguages(List<String> languages){
            this.languages = languages;
        }
    
        public List<String> getLanguages(){
            return languages;
        }
    
        public void setListImage(String listImage){
            this.listImage = listImage;
        }
    
        public String getListImage(){
            return listImage;
        }
    
        public void setAgeRating(String ageRating){
            this.ageRating = ageRating;
        }
    
        public String getAgeRating(){
            return ageRating;
        }
    
        public void setOrderid(int orderid){
            this.orderid = orderid;
        }
    
        public int getOrderid(){
            return orderid;
        }
    
        public void setImageUrl(String imageUrl){
            this.imageUrl = imageUrl;
        }
    
        public String getImageUrl(){
            return imageUrl;
        }
    
        public void setEndCreditsStartS(String endCreditsStartS){
            this.endCreditsStartS = endCreditsStartS;
        }
    
        public String getEndCreditsStartS(){
            return endCreditsStartS;
        }
    
        public void setContentOwner(String contentOwner){
            this.contentOwner = contentOwner;
        }
    
        public String getContentOwner(){
            return contentOwner;
        }
    
        public void setTags(List<String> tags){
            this.tags = tags;
        }
    
        public List<String> getTags(){
            return tags;
        }
    
        public void setChannels(List<ChannelsItem> channels){
            this.channels = channels;
        }
    
        public List<ChannelsItem> getChannels(){
            return channels;
        }
    
        public void setWebUrl(String webUrl){
            this.webUrl = webUrl;
        }
    
        public String getWebUrl(){
            return webUrl;
        }
    
        public void setReleaseDate(String releaseDate){
            this.releaseDate = releaseDate;
        }
    
        public String getReleaseDate(){
            return releaseDate;
        }
    
        @Override
        public String toString(){
            return 
                "AssetDetails{" + 
                "asset_subtype = '" + assetSubtype + '\'' + 
                ",audio_languages = '" + audioLanguages + '\'' + 
                ",subtitle_languages = '" + subtitleLanguages + '\'' + 
                ",rating = '" + rating + '\'' + 
                ",description = '" + description + '\'' + 
                ",title = '" + title + '\'' + 
                ",drm_key_id = '" + drmKeyId + '\'' + 
                ",extended = '" + extended + '\'' + 
                ",duration = '" + duration + '\'' + 
                ",episode_number = '" + episodeNumber + '\'' + 
                ",video_url = '" + videoUrl + '\'' + 
                ",subtitle_lang = '" + subtitleLang + '\'' + 
                ",genres = '" + genres + '\'' + 
                ",business_type = '" + businessType + '\'' + 
                ",asset_type = '" + assetType + '\'' + 
                ",tvshow = '" + tvshow + '\'' + 
                ",season = '" + season + '\'' + 
                ",id = '" + id + '\'' + 
                ",skip_available = '" + skipAvailable + '\'' + 
                ",cover_image = '" + coverImage + '\'' + 
                ",slug = '" + slug + '\'' + 
                ",season_details = '" + seasonDetails + '\'' + 
                ",original_title = '" + originalTitle + '\'' + 
                ",is_drm = '" + isDrm + '\'' + 
                ",languages = '" + languages + '\'' + 
                ",list_image = '" + listImage + '\'' + 
                ",age_rating = '" + ageRating + '\'' + 
                ",orderid = '" + orderid + '\'' + 
                ",image_url = '" + imageUrl + '\'' + 
                ",end_credits_start_s = '" + endCreditsStartS + '\'' + 
                ",content_owner = '" + contentOwner + '\'' + 
                ",tags = '" + tags + '\'' + 
                ",channels = '" + channels + '\'' + 
                ",web_url = '" + webUrl + '\'' + 
                ",release_date = '" + releaseDate + '\'' + 
                "}";
            }
    }

共有1个答案

司寇凯
2023-03-14

您可以使用自定义反序列化器来检查该值是否为真实对象,并决定如果不是该值应如何处理。

例如(用于任何类型):

public class StringOrObjectDeSerializer<T> implements JsonDeserializer<T> {
    // deserializer NOT registered with this Gson to prevent recursion!
    private final Gson innerGson = new Gson();

    @Override
    public T deserialize(JsonElement json, Type typeOfT, 
            JsonDeserializationContext context)
            throws JsonParseException {
        if (json.isJsonObject()) {
            return innerGson.fromJson(json, typeOfT);
        }
        // null or some other default value for non-json-objects
        return null;
    }
}

只需在使用前注册,比如:

Gson gson = new GsonBuilder()
    .registerTypeAdapter(SeasonDetail.class, new StringOrObjectDeSerializer<>())
    .create();
 类似资料:
  • 我是新来改装的。我已经在大多数Web服务中使用了volley,决定继续改进。但是我被Json解析所困,我在POJO类中得到了空数据。我尝试通过检查响应,我的响应是正确的,当我在模型类中解析时,我得到的是空白数据。请指导我,任何帮助都将不胜感激。 注意:POJO类是可parcelable,还包含cunstructor和gette/setter。我从这里删除了它,因为文本限制。 JSON响应

  • 但是我的DetailModel是一个基类,我的user.class和organization.class是DetailModel的子类。但是我不知道如何解析对我的用户或组织模型的响应。

  • 错误是: 方法引发了'java.lang.NullPointerException'异常。无法计算pl.netizens.eonbeacon.storage.eonbeaconmodel.ToString() Eeid,Eeid模型:

  • 我不知道为什么我的json解析不起作用。这是我正在使用的Api。还有完整json输出的链接http://api.openweathermap.org/data/2.5/forecast/daily?zip=85008 我试图得到每天的最低和最高温度。这是我的代码。引发的异常应该是BEGIN_OBJECT,但在第1行第190列编号。任何帮助不胜感激,谢谢!

  • 问题内容: 我真的很head头。我使用s已有一段时间了,但现在,使用SimpleDateFormat解析日期(只是有时)是完全错误的。 特别: 打印字符串。有没有搞错?-它甚至都不会一直解析为错误的日期! 更新: 修复得很漂亮。您不知道吗,在其他一些地方也滥用了它。一定喜欢调试别人的代码:) 问题答案: 我认为您要使用格式,而不是’hh’,以便您使用00-23之间的小时数。“ hh”采用12小时增

  • 获取此错误:Java . lang . runtime exception:org . simple framework . XML . core . method exception:Annotation @ org . simple framework . XML . element(data = false,name=,required=false,type=void)必须标记set或get