繁体   English   中英

使用杰克逊解析json,无法从pojos中获取和检索值

[英]Parsing json using jackson and not able to reach and retrieve values from pojos

我对jackson真的很陌生,我正在尝试从json响应中检索值。 这是我的json响应:-

{
total: "1",
count: "1",
params: {
res: "all",
detail: "summary",
smartCardId: "S1234567890",
receiverId: "r1234567890",
format: "all",
lang: "en",
storefrontType: "EN",
tcsMasterId: "41S7V"
},
baseUrl: {
asset: "http://10.84.90.186/content/video/",
poster: "http://10.84.90.186/content"
},
items: [
{
contentType: "ASSET",
endPointUrl: "/assets/41S7V",
showType: "MOVIE",
tcsMasterId: "41S7V",
title: "300: Rise of an Empire",
titleBrief: "300: Rise of an Empire",
summaryShort: "l ",
summaryMedium: "l ",
summaryLong: "l ",
year: "2014",
rating: "G",
runtime: "00:14:59",
displayRuntime: "14",
suggestedPrice: "5.99",
providerId: "www.mongrelmedia.com",
maximumViewingLength: "2880",
audioType: [
"Dolby Digital 5.1"
],
licensingWindowStart: "2015-01-01T00:00:00",
licensingWindowEnd: "2015-12-31T12:00:00",
genres: [
"Movies/Action and Adventure"
],
actors: [
" d"
],
actorsDisplay: "d",
reviewRating: {
type: "ROTTEN_TOMATOES",
criticSummaryCount: "55",
criticSummaryValue: "71",
criticSummaryRating: "3.0",
fanSummaryCount: "47103",
fanSummaryValue: "88",
fanSummaryRating: "4.0",
fanSummaryPreRelease: false,
reviews: [
{
reviewId: "1727471",
publicationId: "0",
publication: "Newsweek",
date: "2008-05-12",
freshnessScore: "null",
freshness: "rotten",
criticId: "0",
criticName: "David Ansen",
quote: "For all its ambition, the movie ends up using great        historical events in the service of a dubious sentimentality."
},
{
reviewId: "1710100",
publicationId: "0",
publication: "Film4",
date: "2008-02-20",
freshnessScore: "4/5",
freshness: "fresh",
criticId: "0",
quote: "Unashamedly sentimental, this is a technically triumphant        tear-jerker."
}
]
},
rightsIconDisplay: "IPT=1,DTH=0,WEB=0,NSC=1,MOB=0",
wishList: false,
preview: {
assetId: "41S8V21",
type: "PREVIEW",
contentPath: "www.mongrelmedia.com/41S8V21_a4289887-5f7c-47a4-afad-842c934025e1/MPL_41S8V21_a4289887-5f7c-47a4-afad-842c934025e1.m3u8",
masterPlaylist: "MPL_41S8V21_a4289887-5f7c-47a4-afad-842c934025e1.m3u8",
runtime: "00:14:59"
}
}
]
}

我正在尝试从以下items: []检索值:- items: []例如displayRuntime: "14",并且genre不高,

这是pojo的主要课程:-

 public class AssetGen{
        private BaseUrl baseUrl;
        private String count;
        private List items;
        private Params params;
        private String total;

        public BaseUrl getBaseUrl(){
            return this.baseUrl;
        }
        public void setBaseUrl(BaseUrl baseUrl){
            this.baseUrl = baseUrl;
        }
        public String getCount(){
            return this.count;
        }
        public void setCount(String count){
            this.count = count;
        }
        public List getItems(){
            return this.items;
        }
        public void setItems(List items){
            this.items = items;
        }
        public Params getParams(){
            return this.params;
        }
        public void setParams(Params params){
            this.params = params;
        }
        public String getTotal(){
            return this.total;
        }
        public void setTotal(String total){
            this.total = total;
        }
    }

这是item pojo:-

public class Items{
    private List actors;
    private String actorsDisplay;
    private List audioType;
    private String contentType;
    private String displayRuntime;
    private String endPointUrl;
    private List genres;
    private String licensingWindowEnd;
    private String licensingWindowStart;
    private String maximumViewingLength;
    private Preview preview;
    private String providerId;
    private String rating;
    private ReviewRating reviewRating;
    private String rightsIconDisplay;
    private String runtime;
    private String showType;
    private String suggestedPrice;
    private String summaryLong;
    private String summaryMedium;
    private String summaryShort;
    private String tcsMasterId;
    private String title;
    private String titleBrief;
    private boolean wishList;
    private String year;

    public List getActors(){
        return this.actors;
    }
    public void setActors(List actors){
        this.actors = actors;
    }
    public String getActorsDisplay(){
        return this.actorsDisplay;
    }
    public void setActorsDisplay(String actorsDisplay){
        this.actorsDisplay = actorsDisplay;
    }
    public List getAudioType(){
        return this.audioType;
    }
    public void setAudioType(List audioType){
        this.audioType = audioType;
    }
    public String getContentType(){
        return this.contentType;
    }
    public void setContentType(String contentType){
        this.contentType = contentType;
    }
    public String getDisplayRuntime(){
        return this.displayRuntime;
    }
    public void setDisplayRuntime(String displayRuntime){
        this.displayRuntime = displayRuntime;
    }
    public String getEndPointUrl(){
        return this.endPointUrl;
    }
    public void setEndPointUrl(String endPointUrl){
        this.endPointUrl = endPointUrl;
    }
    public List getGenres(){
        return this.genres;
    }
    public void setGenres(List genres){
        this.genres = genres;
    }
    public String getLicensingWindowEnd(){
        return this.licensingWindowEnd;
    }
    public void setLicensingWindowEnd(String licensingWindowEnd){
        this.licensingWindowEnd = licensingWindowEnd;
    }
    public String getLicensingWindowStart(){
        return this.licensingWindowStart;
    }
    public void setLicensingWindowStart(String licensingWindowStart){
        this.licensingWindowStart = licensingWindowStart;
    }
    public String getMaximumViewingLength(){
        return this.maximumViewingLength;
    }
    public void setMaximumViewingLength(String maximumViewingLength){
        this.maximumViewingLength = maximumViewingLength;
    }
    public Preview getPreview(){
        return this.preview;
    }
    public void setPreview(Preview preview){
        this.preview = preview;
    }
    public String getProviderId(){
        return this.providerId;
    }
    public void setProviderId(String providerId){
        this.providerId = providerId;
    }
    public String getRating(){
        return this.rating;
    }
    public void setRating(String rating){
        this.rating = rating;
    }
    public ReviewRating getReviewRating(){
        return this.reviewRating;
    }
    public void setReviewRating(ReviewRating reviewRating){
        this.reviewRating = reviewRating;
    }
    public String getRightsIconDisplay(){
        return this.rightsIconDisplay;
    }
    public void setRightsIconDisplay(String rightsIconDisplay){
        this.rightsIconDisplay = rightsIconDisplay;
    }
    public String getRuntime(){
        return this.runtime;
    }
    public void setRuntime(String runtime){
        this.runtime = runtime;
    }
    public String getShowType(){
        return this.showType;
    }
    public void setShowType(String showType){
        this.showType = showType;
    }
    public String getSuggestedPrice(){
        return this.suggestedPrice;
    }
    public void setSuggestedPrice(String suggestedPrice){
        this.suggestedPrice = suggestedPrice;
    }
    public String getSummaryLong(){
        return this.summaryLong;
    }
    public void setSummaryLong(String summaryLong){
        this.summaryLong = summaryLong;
    }
    public String getSummaryMedium(){
        return this.summaryMedium;
    }
    public void setSummaryMedium(String summaryMedium){
        this.summaryMedium = summaryMedium;
    }
    public String getSummaryShort(){
        return this.summaryShort;
    }
    public void setSummaryShort(String summaryShort){
        this.summaryShort = summaryShort;
    }
    public String getTcsMasterId(){
        return this.tcsMasterId;
    }
    public void setTcsMasterId(String tcsMasterId){
        this.tcsMasterId = tcsMasterId;
    }
    public String getTitle(){
        return this.title;
    }
    public void setTitle(String title){
        this.title = title;
    }
    public String getTitleBrief(){
        return this.titleBrief;
    }
    public void setTitleBrief(String titleBrief){
        this.titleBrief = titleBrief;
    }
    public boolean getWishList(){
        return this.wishList;
    }
    public void setWishList(boolean wishList){
        this.wishList = wishList;
    }
    public String getYear(){
        return this.year;
    }
    public void setYear(String year){
        this.year = year;
    }
}

这是我要使用的杰克逊代码:-

@SuppressWarnings("rawtypes")
    public String[] getShowNWdetailsGopherParser (String url) throws JsonParseException, JsonMappingException, IOException{

        System.err.println("STAGE 1");
        URL jsonUrl = new URL(url);
        ObjectMapper objmapper = new ObjectMapper();
        objmapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        List<AssetGen> jsongenShow_1 = objmapper.readValue(jsonUrl, new TypeReference <List<AssetGen>>() {});
        List [] shows = new List [jsongenShow_1.size()]; 
        String[] showsArr = new String[jsongenShow_1.size()];
        int i = 0;
        for(AssetGen element : jsongenShow_1){
            shows[i]=element.getItems().
            showsArr[i]=shows[i].toString();
            i++;
        }
        return showsArr;    
    }

需要明确的解决方案,对杰克逊来说真的是新的。

Items是一个数组。 我认为如果将POJO从Items重命名为Item会更清楚。

因此,要在单个项目中获得价值,它应该是这样的:

 for(AssetGen element : jsongenShow_1){
        List<Item> items=element.getItems();
        for (Item item:items) {
            //get item value
           item.getRuntime();
           ....
        }

    }

这些是我必须在主POJO类中进行的更改:

public List<Items> getItems(){
        return this.items;
    }
    public void setItems(List<Items> items){
        this.items = items;
    }

我使用下面给定的杰克逊函数从Item检索值:-

public String getDisplayRuntimer (String url) throws JsonParseException, JsonMappingException, IOException{


        String hello = null;
        URL jsonUrl = new URL(url);
        ObjectMapper objmapper = new ObjectMapper();
        objmapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
         AssetGen jsongenShow_1 = objmapper.readValue(jsonUrl, AssetGen.class);
         hello = jsongenShow_1.getItems().get(0).getDisplayRuntime();
         return hello;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM