Codehaus Jackson JSON数据到POJO无法反序列化实例......超出START_ARRAY标记

时间:2015-10-14 07:06:57

标签: java json serialization jackson deserialization

我使用Codehaus Jackson将json数据转换为带有对象数组的POJO。 我遇到了对象数组的问题。我收到的错误如“无法反序列化实例......超出START_ARRAY标记”。

以下是我的代码

JSON数据(flightItineraryPrice.json)

{  
    "tripType": "OneWay",  
    "tripInfos":[  
        {  
            "from":"EARTH",  
            "to":"MOON",  
            "fromSchedule":"2015-12-21T04:30:00",  
            "toSchedule":"2015-12-21T06:50:00"  
        },  
        {  
            "from":"MOON",  
            "to":"MARS",  
            "fromSchedule":"2015-12-21T03:30:00",  
            "toSchedule":"2015-12-21T011:10:00"  
        },  
        {  
            "from":"VENUS",  
            "to":"KEPLER",  
            "fromSchedule":"2015-12-21T01:30:00",  
            "toSchedule":"2015-12-21T22:30:00"  
        },     
        {     
            "from":"EARTH",  
            "to":"SUN",  
            "fromSchedule":"2015-12-20T02:30:00",  
            "toSchedule":"2015-12-29T15:10:00"  
        }  
    ],  
    "adultFare":{  
        "paxType":"ADT",  
        "baseFare":"1000",  
        "totalFeesAndTaxes":"300",  
        "totalAmount":"1300.00"  
    },  
    "childFare":{  
        "paxType":"CHD",  
        "baseFare":"750",  
        "totalFeesAndTaxes":"250",  
        "totalAmount":"1000.00"  
    },  
    "infantFare":{  
        "paxType":"INF",  
        "baseFare":"250",  
        "totalFeesAndTaxes":"25",  
        "totalAmount":"275.00"  
    },  
    "adultCount":"1",  
    "childCount":"1",  
    "infantCount":"2"  
}  

班级(JacksonFlightItineraryPrice.java)

package com.jgtt.samples;  

import java.util.*;  
import org.codehaus.jackson.annotate.JsonProperty;  

public class JacksonFlightItineraryPrice {  

    private String tripType;  

    @JsonProperty("tripInfos")  
    private JacksonFlightItineraryPrice.TripInfo tripInfos;  

    private JacksonFlightItineraryPrice.PaxFare adultFare;  
    private JacksonFlightItineraryPrice.PaxFare childFare;  
    private JacksonFlightItineraryPrice.PaxFare infantFare;  

    private short adultCount;  
    private short childCount;  
    private short infantCount;  

    public JacksonFlightItineraryPrice() {}  


    public String getTripType() {  
        return (this.tripType);   
    }     
    public void setTripType(String tripType) {  
        this.tripType = tripType;   
    }  

    public JacksonFlightItineraryPrice.TripInfo getTripInfos() {  
        return (this.tripInfos);  
    }  
    public void setTripInfos(JacksonFlightItineraryPrice.TripInfo tripInfos) {  
        this.tripInfos = tripInfos;   
    }  

    public JacksonFlightItineraryPrice.PaxFare getAdultFare() {  
        return (this.adultFare);   
    }     
    public void setAdultFare(JacksonFlightItineraryPrice.PaxFare adultFare) {  
        this.adultFare = adultFare;   
    }  

    public JacksonFlightItineraryPrice.PaxFare getChildFare() {  
        return (this.childFare);   
    }   
    public void setChildFare(JacksonFlightItineraryPrice.PaxFare childFare) {  
        this.childFare = childFare;   
    }  

    public JacksonFlightItineraryPrice.PaxFare getInfantFare() {  
        return (this.infantFare);   
    }     
    public void setInfantFare(JacksonFlightItineraryPrice.PaxFare infantFare) {  
        this.infantFare = infantFare;   
    }  


    public short getAdultCount() {  
        return (this.adultCount);  
    }  
    public void setAdultCount(short adultCount) {  
        this.adultCount = adultCount;   
    }  

    public short getChildCount() {  
        return (this.childCount);  
    }  
    public void setChildCount(short childCount) {  
        this.childCount = childCount;   
    }  

    public short getInfantCount() {  
        return (this.infantCount);  
    }   
    public void setInfantCount(short infantCount) {  
        this.infantCount = infantCount;   
    }  


    public static class TripInfo {  
        private List<JacksonFlightItineraryPrice.FlightInfoParameter> flightInfoParameters;  

        public List getFlightInfoParameters() {  
            return (this.flightInfoParameters);  
        }  
        public void setFlightInfoParameters(List flightInfoParameters) {  
            this.flightInfoParameters = flightInfoParameters;   
        }  

        public String toString() {  
            StringBuilder sb = new StringBuilder();  
            sb.append("[\n");  
            if(null != flightInfoParameters){  
                sb.append("  flightInfoParameters:\n");  
                boolean isFirst = true;  
                for(FlightInfoParameter f : flightInfoParameters){  
                    if(!isFirst){  
                        sb.append(",\n");     
                    }  
                    sb.append(f);  
                    isFirst = false;  
                }  
                sb.append("\n");  
            }  
            else {  
                sb.append("  flightInfoParameters=null\n");  
            }  
            sb.append("]");  
            return sb.toString();  
        }  
    }  


    public static class FlightInfoParameter {  

        private String from;  
        private String to;  
        private String fromSchedule;    
        private String toSchedule;  

        public String getFrom() {  
            return from;   
        }  
        public void setFrom(String from) {  
            this.from = from;   
        }  

        public String getTo() {  
            return to;   
        }  
        public void setTo(String to) {  
            this.to = to;   
        }         

        public String getFromSchedule() {  
            return fromSchedule;   
        }  
        public void setFromSchedule(String fromSchedule) {  
            this.fromSchedule = fromSchedule;   
        }   

        public String getToSchedule() {  
            return toSchedule;   
        }  
        public void setToSchedule(String toSchedule) {  
            this.toSchedule = toSchedule;   
        }  

        @Override  
        public String toString() {  
            return "User [ "+from+"(" + fromSchedule + ") -> " + to + "(" + toSchedule +  ") ]";  
        }  
    }  


    public static class PaxFare {  

        private String paxType;  
        private Double baseFare;  
        private Double totalFeesAndTaxes;  
        private Double totalAmount;  

        public String getPaxType() {  
            return paxType;   
        }  
        public void setPaxType(String paxType) {  
            this.paxType = paxType;   
        }  

        public Double getBaseFare() {  
            return baseFare;   
        }  
        public void setBaseFare(Double baseFare) {  
            this.baseFare = baseFare;   
        }  

        public Double getTotalFeesAndTaxes() {  
            return totalFeesAndTaxes;   
        }  
        public void setTotalFeesAndTaxes(Double totalFeesAndTaxes) {  
            this.totalFeesAndTaxes = totalFeesAndTaxes;   
        }  

        public Double getTotalAmount() {  
            return totalAmount;   
        }  
        public void setTotalAmount(Double totalAmount) {  
            this.totalAmount = totalAmount;   
        }  

        @Override  
        public String toString() {  
            return "User [paxType=" + paxType + ", baseFare=" + baseFare + ", totalFeesAndTaxes" + totalFeesAndTaxes +  ", totalAmount=" + totalAmount + "]";  
        }  
    }  

    @Override  
    public String toString() {  
        StringBuilder sb = new StringBuilder();  
        sb.append("[\n");  
        sb.append("  tripType=" + tripType + ",\n");  
        sb.append("  tripInfos=" + tripInfos + ",\n");  
        sb.append("  adultFare=" + adultFare + ",\n");  
        sb.append("  childFare=" + childFare + ",\n");  
        sb.append("  infantFare=" + infantFare + ",\n");  
        sb.append("  adultCount=" + adultCount + ",\n");  
        sb.append("  childCount=" + childCount + ",\n");  
        sb.append("  infantCount=" + infantCount + "\n]");  
        return sb.toString();  
    }  
}  

主要应用程序(JacksonExample.java)

package com.jgtt.samples;  

import java.util.*;  
import java.io.File;  
import java.io.IOException;  
import org.codehaus.jackson.JsonGenerationException;  
import org.codehaus.jackson.map.ObjectMapper;  
import org.codehaus.jackson.map.JsonMappingException;  
import org.codehaus.jackson.type.TypeReference;  
import org.codehaus.jackson.map.type.CollectionType;  
import org.codehaus.jackson.map.type.TypeFactory;  


public class JacksonExample {  

    private static void flightJsonToPojo(){  
        ObjectMapper mapper = new ObjectMapper();   
        try {   
            String file_url = "d:\\Work files\\Java Test Codes\\Jackson\\flightItineraryPrice.json";  
            File jsonFile = new File(file_url);  
            JacksonFlightItineraryPrice flightInfo = mapper.readValue(jsonFile, JacksonFlightItineraryPrice.class);  

            System.out.println(flightInfo);   
        } catch (JsonGenerationException e) {   
            e.printStackTrace();    
        } catch (JsonMappingException e) {  
            e.printStackTrace();    
        } catch (IOException e) {   
            e.printStackTrace();    
        }  
    }  

    public static void main(String[] args) {  
        flightJsonToPojo();  
    }  


}  

我遇到问题的json数据中唯一的部分是“tripInfos”,因为它的值是对象的arraylist。如果我删除tripInfos ......一切正常......但我真的也需要它。

希望有人能给我指路:)

1 个答案:

答案 0 :(得分:1)

班级JacksonFlightItineraryPrice有一个属性tripInfos,不是数组:

@JsonProperty("tripInfos")  
private JacksonFlightItineraryPrice.TripInfo tripInfos; 

TripInfo flightInfoParameters ListList<JacksonFlightItineraryPrice.FlightInfoParameter>

但是你的json tripInfos是一个JacksonFlightItineraryPrice.FlightInfoParameter的数组。

相反它应该是:

{  
    "tripType": "OneWay",  
    "tripInfos": {
        "flightInfoParameters" : [  
            {  
                "from":"EARTH",  
                "to":"MOON",  
                "fromSchedule":"2015-12-21T04:30:00",  
                "toSchedule":"2015-12-21T06:50:00"  
            },  
            {  
                "from":"MOON",  
                "to":"MARS",  
                "fromSchedule":"2015-12-21T03:30:00",  
                "toSchedule":"2015-12-21T011:10:00"  
            },  
            {  
                "from":"VENUS",  
                "to":"KEPLER",  
                "fromSchedule":"2015-12-21T01:30:00",  
                "toSchedule":"2015-12-21T22:30:00"  
            },     
            {     
                "from":"EARTH",  
                "to":"SUN",  
                "fromSchedule":"2015-12-20T02:30:00",  
                "toSchedule":"2015-12-29T15:10:00"  
            }  
        ]
    },
    "adultFare":{  
        "paxType":"ADT",  
        "baseFare":"1000",  
        "totalFeesAndTaxes":"300",  
        "totalAmount":"1300.00"  
    },  
    "childFare":{  
        "paxType":"CHD",  
        "baseFare":"750",  
        "totalFeesAndTaxes":"250",  
        "totalAmount":"1000.00"  
    },  
    "infantFare":{  
        "paxType":"INF",  
        "baseFare":"250",  
        "totalFeesAndTaxes":"25",  
        "totalAmount":"275.00"  
    },  
    "adultCount":"1",  
    "childCount":"1",  
    "infantCount":"2"  
} 

或者如果您无法更改JSON,那么在JacksonFlightItineraryPrice中,属性tripInfos应该是FlightInfoParameter的列表:

@JsonProperty("tripInfos")  
private List<JacksonFlightItineraryPrice.FlightInfoParameter> tripInfos;