Java 正在使用jackson解析json,但无法从POJO获取值

Java 正在使用jackson解析json,但无法从POJO获取值,java,json,jackson,Java,Json,Jackson,我对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:

我对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"
}
}
]
}
我正在尝试从:-项:[]检索值,例如displayRuntime:14和流派,但无法访问它-

以下是主要的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;
        }
    }
以下是项目pojo:-

以下是我尝试使用的jackson代码:-

@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;    
    }
需要明确的解决方案,这对jackson来说是一个新问题。

项目是一个数组。 我认为,如果您将POJO从一个项目重命名为另一个项目,将会更加清楚

因此,要在单个项目中获得价值,应该如下所示:

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

它仍然不工作-List jsongenShow_1=objmapper.readValuejsonUrl,new TypeReference{};列表[]显示=新列表[jsongenShow_1.size];字符串[]showsArr=新字符串[jsongenShow_1.size];int i=0;对于AssetGen元素:jsongenShow_1{List items=element.getItems;对于items项:items{//get item value showsArr[i]=item.getRuntime;i++;}线程main com.fasterxml.jackson.databind.JsonMappingException:无法在[Source:;line:1,column:1]处从START_对象标记反序列化java.util.ArrayList的实例解析json时,必须在这一行发生此错误:`List jsongenShow_1=objmapper.readValuejsonUrl,new TypeReference{};`。您的映射不正确。只要有列表,就将其更改为数组。根据您提供的url上的json判断,您需要传递给ObjectMapper的类是AssetGen,而不是对列表的TypeReference感谢您的帮助,这些更改在我的主pojo类:public list getItems{return this.items;}public void setItemsList items{this.items=items;}使其正常工作
public List<Items> getItems(){
        return this.items;
    }
    public void setItems(List<Items> items){
        this.items = items;
    }
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;
    }