如何从JSON pojo获取值

时间:2015-01-13 23:23:54

标签: java json pojo

我是JSON和jackson的新手,目前我有pojo文件,我正在尝试获取数据并将其存储在数组中。例如,我想提取网络名称并将其存储为一个数组,然后显示或与实时网站数据进行比较。

这是主要的pojo文件 -

    public class JsonGen{
        private String _type;
        private List cast;
        private List clips;
        private Common_sense_data common_sense_data;
        private String common_sense_id;
        private List crew;
        private String description;
        private List episodes;
        private Number franchise_id;
        private List genres;
        private String guid;
        private Images images;
        private boolean is_locked;
        private boolean is_mobile;
        private boolean is_parental_locked;
        private String kind;
        private List mobile_networks;
        private String most_recent_full_episode_added_date;
        private String name;
        private List networks;
        private List platforms;
        private List ratings;
        private String release_date;
        private List season_filters;
        private String slug;
        private String tms_id;

        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 List getClips(){
            return this.clips;
        }
        public void setClips(List clips){
            this.clips = clips;
        }
        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 List getEpisodes(){
            return this.episodes;
        }
        public void setEpisodes(List episodes){
            this.episodes = episodes;
        }
        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 getMobile_networks(){
            return this.mobile_networks;
        }
        public void setMobile_networks(List 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){
            this.networks = networks;
        }
        public List getPlatforms(){
            return this.platforms;
        }
        public void setPlatforms(List 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;
        }

    }

这是Network Pojo类 -

 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;
        }
        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 class util {

        public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {

            List<JsonGen> jsongenShow = null;
            String url1 = "http://www.dishanywhere.com/radish/v20/dol/home/carousels/shows.json";
            getShowNWGopherParser(nwork, url1);
        }

    public static String[] getShowNWGopherParser (List<Networks> nwork, String url ) throws JsonParseException, JsonMappingException, IOException
    {
        URL jsonUrl = new URL(url);
        ObjectMapper objmapper = new ObjectMapper();
        //objmapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        nwork = objmapper.readValue(jsonUrl, new TypeReference<List<Networks>>() {});
        String [] shows = new String [nwork.size()]; 
        int i = 0;
        for(Networks element : nwork) {
            shows[i++]=element.getUrl();
            }
        for(int j =0; j<shows.length;j++)
        {
            System.out.println(shows[j]);

        }
        return shows;   
     }
    }

这是错误 -

 Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "networks" (class featureshows.Networks), not marked as ignorable (10 known properties: , "logo", "slug", "name", "banner", "network_id", "url", "network_analytics", "description", "thumbnail_url", "is_locked"])
     at [Source: http://www.dishanywhere.com/radish/v20/dol/home/carousels/shows.json; line: 1, column: 15] (through reference chain: featureshows.Networks["networks"])
        at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:79)
        at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:568)
        at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:650)
        at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:830)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:310)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:112)
        at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:226)
        at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:203)
        at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:23)
        at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2563)
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1789)
        at functions.util.getShowNWGopherParser(util.java:77)
        at functions.util.main(util.java:31)

1 个答案:

答案 0 :(得分:0)

原因是您使用常规List接口作为网络列表,而不是具有预期类型边界的通用版本。

尝试从以下位置更改字段声明:

private List networks;

private List<Networks> networks;

当你在这里时,看起来你几乎无处不在地使用常规的List界面。如果不将它们全部转换为包含列表中预期的类型,则可能会遇到更多问题。

基本上,你没有提供足够的关于你在列表中所期望的对象类型的信息,以便杰克逊弄清楚要填充它的内容。您可以在此处详细了解泛型:http://docs.oracle.com/javase/tutorial/extra/generics/intro.html

修改

看起来(根据您的评论)您已尝试禁用对未知属性的检查,但请尝试将此注释添加到您的Networks类:

@JsonIgnoreProperties(ignoreUnknown = true)