借鉴自:http://www.jb51.net/article/91142.htm
在将json字符串转换成对象flightInfo时,当flightInfo对象中有List<flightClassInfo>类型的字段时,使用阿里巴巴的JSON.parse(jsonString, FlightInfo.class)时,其内部的list<flightClassInfo>没有转换成功,而是转换成HashMap结构。因此使用JSONObject对象;
1.maven依赖
<dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency>
2.java代码
1 try{ 2 JSONObject jsonObject = JSONObject.fromObject(jsonFlight); 3 Map<String,Class> map = new HashMap<String,Class>(); 4 map.put("bingoClassInfoList", FlightClassInfo.class); 5 FlightInfo flightInfo = (FlightInfo) JSONObject.toBean(jsonObject, FlightInfo.class, map); 6 List<FlightInfo> flights = new ArrayList<FlightInfo>(); 7 flights.add(flightInfo); 8 filterCharterFlight(flights); 9 System.out.println(flights.toString()); 10 }catch (Exception e){ 11 e.printStackTrace(); 12 }
其中FlightInfo类如下:
1 import java.io.Serializable; 2 import java.util.List; 3 4 public class FlightInfo implements Serializable { 5 6 private static final long serialVersionUID = 8604216922911620556L; 7 /** 8 * 航班信息属性 9 */ 10 public String airways; 11 12 public String airwaysCn; 13 14 public String arrAirdrome; 15 16 public String arrCity; 17 18 public String arrCityCn; 19 20 public String arrDate; 21 22 public String arrTerminal; 23 24 public String arrTime; 25 26 public boolean asr; 27 28 public String carrier; 29 30 public String carrierCn; 31 32 public long childfee; 33 34 public List<FlightClassInfo> bingoClassInfoList; 35 。。。。。
FlightClassInfo类对象如下:
1 import java.io.Serializable; 2 import java.util.List; 3 4 public class FlightClassInfo implements Serializable { 5 6 private static final long serialVersionUID = 2932213372331900441L; 7 8 public String c; 9 10 public double agentFee; 11 12 public double agentFeeGo; 13 14 public long avItemTax; 15 16 public String basicCabinRef; 17 18 public long childOilTax; 19 20 public long childPrice; 21 22 public String childfarebasis; 23 24 public String classNo; 25 26 public String classNoCn; 27 。。。。。。
json字符串如下
String jsonFlight = "{ " + " "oldminPrice": 0, " + " "depTerminal": "T3", " + " "minDiscount": 0, " + " "earningLevel": 0, " + " "asr": false, " + " "sourceId": "661726", " + " "arrCity": "HFE", " + " "childfee": 0, " + " "flightNo": "ZH9899", " + " "planeStyle": "320", " + " "arrTime": "0025", " + " "codeShare": false, " + " "arrDate": "2018-02-05", " + " "depTime": "2215", " + " "depDate": "2018-02-04", " + " "isStop": "0", " + " "airways": "ZH", " + " "depAirdrome": "宝安机场", " + " "depCity": "SZX", " + " "exsitsActivity": false, " + " "fee": 0, " + " "tax": 50, " + " "arrAirdrome": "新桥机场", " + " "originalMinPrice": 0, " + " "arrTerminal": "机场", " + " "fullPrice": 1240, " + " "bingoClassInfoList": [{ " + " "airCompanyFlag": true, " + " "activityId": 0, " + " "infantVenderPrice": 0, " + " "policyId": "v/UmAGQoE1+A3k+c8S1V75wUyW4oMfSQSp/WJesbrOPuaxFl82q79w==", " + " "rfId": 0, " + " "netChildPrice": 0, " + " "agentFee": 0.0, " + " "promotionReducePrice": 0, " + " "fareItemId": "jdselfbspZHZH98992018-02-04F", " + " "discount": 250, " + " "fee": 0, " + " "subsidy": 0, " + " "childSeatCode": "F", " + " "price": 0, " + " "originalPrice": 3100, " + " "infantSalePrice": 0, " + " "newMinPrice": 0, " + " "oilTax": 0, " + " "classLevel": "F", " + " "zvItemId": 0, " + " "customerId": 0, " + " "maxPassengerNum": 0, " + " "childOilTax": 0, " + " "childVenderPrice": 1550, " + " "sortPrice": 0, " + " "minPassengerNum": 0, " + " "rtDiscount": 0, " + " "fcClassType": "5", " + " "classNoCn": "", " + " "zvItemIdGo": 0, " + " "promotionBeans": 0, " + " "skuId": 0, " + " "agentFeeGo": 0.0, " + " "venderPrice": 3100, " + " "oldDiscount": 0, " + " "promotionPrice": 0.0, " + " "fullPrice": 1240, " + " "discountFlag": false, " + " "luggallow": "", " + " "childPrice": 0, " + " "avItemTax": 0, " + " "deficitPrice": 3100, " + " "resetPolicy": false, " + " "uniqueKey": "v/UmAGQoE1+A3k+c8S1V75wUyW4oMfSQSp/WJesbrOPuaxFl82q79w==", " + " "sourceId": "661726", " + " "refundFeeFormulaId": 0, " + " "classNo": "F", " + " "pat": false, " + " "iOilTax": 0, " + " "seatNum": "6", " + " "oldprice": 0, " + " "productCode": "", " + " "netPrice": 0, " + " "childSalePrice": 0, " + " "reducePrice": 0, " + " "exsitsActivity": false " + " }, { " + " "airCompanyFlag": true, " + " "activityId": 0, " + " "infantVenderPrice": 0, " + " "policyId": "v/UmAGQoE1+A3k+c8S1V75wUyW4oMfSQL5O7RthR+czuaxFl82q79w==", " + " "rfId": 0, " + " "netChildPrice": 0, " + " "agentFee": 0.0, " + " "promotionReducePrice": 0, " + " "fareItemId": "jdselfbspZHZH98992018-02-04C", " + " "discount": 240, " + " "fee": 0, " + " "subsidy": 0, " + " "childSeatCode": "C", " + " "price": 0, " + " "originalPrice": 2980, " + " "infantSalePrice": 0, " + " "newMinPrice": 0, " + " "oilTax": 0, " + " "classLevel": "C", " + " "zvItemId": 0, " + " "customerId": 0, " + " "maxPassengerNum": 0, " + " "childOilTax": 0, " + " "childVenderPrice": 1490, " + " "sortPrice": 0, " + " "minPassengerNum": 0, " + " "rtDiscount": 0, " + " "fcClassType": "5", " + " "classNoCn": "", " + " "zvItemIdGo": 0, " + " "promotionBeans": 0, " + " "skuId": 0, " + " "agentFeeGo": 0.0, " + " "venderPrice": 2980, " + " "oldDiscount": 0, " + " "promotionPrice": 0.0, " + " "fullPrice": 1240, " + " "discountFlag": false, " + " "luggallow": "", " + " "childPrice": 0, " + " "avItemTax": 0, " + " "deficitPrice": 2980, " + " "resetPolicy": false, " + " "uniqueKey": "v/UmAGQoE1+A3k+c8S1V75wUyW4oMfSQL5O7RthR+czuaxFl82q79w==", " + " "sourceId": "661726", " + " "refundFeeFormulaId": 0, " + " "classNo": "C", " + " "pat": false, " + " "iOilTax": 0, " + " "seatNum": "2", " + " "oldprice": 0, " + " "productCode": "", " + " "netPrice": 0, " + " "childSalePrice": 0, " + " "reducePrice": 0, " + " "exsitsActivity": false " + " }, { " + " "airCompanyFlag": true, " + " "activityId": 0, " + " "infantVenderPrice": 0, " + " "policyId": "v/UmAGQoE1+A3k+c8S1V75wUyW4oMfSQ0UENkdxWb2buaxFl82q79w==", " + " "rfId": 0, " + " "netChildPrice": 0, " + " "agentFee": 0.0, " + " "promotionReducePrice": 0, " + " "fareItemId": "jdselfbspZHZH98992018-02-04M", " + " "discount": 96, " + " "fee": 0, " + " "subsidy": 0, " + " "childSeatCode": "Y", " + " "price": 0, " + " "originalPrice": 1190, " + " "infantSalePrice": 0, " + " "newMinPrice": 0, " + " "oilTax": 0, " + " "classLevel": "Y", " + " "zvItemId": 0, " + " "customerId": 0, " + " "maxPassengerNum": 0, " + " "childOilTax": 0, " + " "childVenderPrice": 620, " + " "sortPrice": 0, " + " "minPassengerNum": 0, " + " "rtDiscount": 0, " + " "fcClassType": "5", " + " "classNoCn": "", " + " "zvItemIdGo": 0, " + " "promotionBeans": 0, " + " "skuId": 0, " + " "agentFeeGo": 0.0, " + " "venderPrice": 1190, " + " "oldDiscount": 0, " + " "promotionPrice": 0.0, " + " "fullPrice": 1240, " + " "discountFlag": false, " + " "luggallow": "", " + " "childPrice": 0, " + " "avItemTax": 0, " + " "deficitPrice": 1190, " + " "resetPolicy": false, " + " "uniqueKey": "v/UmAGQoE1+A3k+c8S1V75wUyW4oMfSQ0UENkdxWb2buaxFl82q79w==", " + " "sourceId": "661726", " + " "refundFeeFormulaId": 0, " + " "classNo": "M", " + " "pat": false, " + " "iOilTax": 0, " + " "seatNum": "A", " + " "oldprice": 0, " + " "productCode": "", " + " "netPrice": 0, " + " "childSalePrice": 0, " + " "reducePrice": 0, " + " "exsitsActivity": false " + " }, { " + " "airCompanyFlag": true, " + " "activityId": 0, " + " "infantVenderPrice": 0, " + " "policyId": "v/UmAGQoE1+A3k+c8S1V75wUyW4oMfSQpv8z2L3W34nuaxFl82q79w==", " + " "rfId": 0, " + " "netChildPrice": 0, " + " "agentFee": 0.0, " + " "promotionReducePrice": 0, " + " "fareItemId": "jdselfbspZHZH98992018-02-04H", " + " "discount": 81, " + " "fee": 0, " + " "subsidy": 0, " + " "childSeatCode": "Y", " + " "price": 0, " + " "originalPrice": 1000, " + " "infantSalePrice": 0, " + " "newMinPrice": 0, " + " "oilTax": 0, " + " "classLevel": "Y", " + " "zvItemId": 0, " + " "customerId": 0, " + " "maxPassengerNum": 0, " + " "childOilTax": 0, " + " "childVenderPrice": 620, " + " "sortPrice": 0, " + " "minPassengerNum": 0, " + " "rtDiscount": 0, " + " "fcClassType": "5", " + " "classNoCn": "", " + " "zvItemIdGo": 0, " + " "promotionBeans": 0, " + " "skuId": 0, " + " "agentFeeGo": 0.0, " + " "venderPrice": 1000, " + " "oldDiscount": 0, " + " "promotionPrice": 0.0, " + " "fullPrice": 1240, " + " "discountFlag": false, " + " "luggallow": "", " + " "childPrice": 0, " + " "avItemTax": 0, " + " "deficitPrice": 1000, " + " "resetPolicy": false, " + " "uniqueKey": "v/UmAGQoE1+A3k+c8S1V75wUyW4oMfSQpv8z2L3W34nuaxFl82q79w==", " + " "sourceId": "661726", " + " "refundFeeFormulaId": 0, " + " "classNo": "H", " + " "pat": false, " + " "iOilTax": 0, " + " "seatNum": "A", " + " "oldprice": 0, " + " "productCode": "", " + " "netPrice": 0, " + " "childSalePrice": 0, " + " "reducePrice": 0, " + " "exsitsActivity": false " + " }, { " + " "airCompanyFlag": true, " + " "activityId": 0, " + " "infantVenderPrice": 0, " + " "policyId": "v/UmAGQoE1+A3k+c8S1V75wUyW4oMfSQy40pO0Np5b/uaxFl82q79w==", " + " "rfId": 0, " + " "netChildPrice": 0, " + " "agentFee": 0.0, " + " "promotionReducePrice": 0, " + " "fareItemId": "jdselfbspZHZH98992018-02-04U", " + " "discount": 86, " + " "fee": 0, " + " "subsidy": 0, " + " "childSeatCode": "Y", " + " "price": 0, " + " "originalPrice": 1070, " + " "infantSalePrice": 0, " + " "newMinPrice": 0, " + " "oilTax": 0, " + " "classLevel": "Y", " + " "zvItemId": 0, " + " "customerId": 0, " + " "maxPassengerNum": 0, " + " "childOilTax": 0, " + " "childVenderPrice": 620, " + " "sortPrice": 0, " + " "minPassengerNum": 0, " + " "rtDiscount": 0, " + " "fcClassType": "5", " + " "classNoCn": "", " + " "zvItemIdGo": 0, " + " "promotionBeans": 0, " + " "skuId": 0, " + " "agentFeeGo": 0.0, " + " "venderPrice": 1070, " + " "oldDiscount": 0, " + " "promotionPrice": 0.0, " + " "fullPrice": 1240, " + " "discountFlag": false, " + " "luggallow": "", " + " "childPrice": 0, " + " "avItemTax": 0, " + " "deficitPrice": 1070, " + " "resetPolicy": false, " + " "uniqueKey": "v/UmAGQoE1+A3k+c8S1V75wUyW4oMfSQy40pO0Np5b/uaxFl82q79w==", " + " "sourceId": "661726", " + " "refundFeeFormulaId": 0, " + " "classNo": "U", " + " "pat": false, " + " "iOilTax": 0, " + " "seatNum": "A", " + " "oldprice": 0, " + " "productCode": "", " + " "netPrice": 0, " + " "childSalePrice": 0, " + " "reducePrice": 0, " + " "exsitsActivity": false " + " }, { " + " "airCompanyFlag": true, " + " "activityId": 0, " + " "infantVenderPrice": 0, " + " "policyId": "v/UmAGQoE1+A3k+c8S1V75wUyW4oMfSQ9Rp/VJ4NrDTuaxFl82q79w==", " + " "rfId": 0, " + " "netChildPrice": 0, " + " "agentFee": 0.0, " + " "promotionReducePrice": 0, " + " "fareItemId": "jdselfbspZHZH98992018-02-04Y", " + " "discount": 100, " + " "fee": 0, " + " "subsidy": 0, " + " "childSeatCode": "Y", " + " "price": 0, " + " "originalPrice": 1240, " + " "infantSalePrice": 0, " + " "newMinPrice": 0, " + " "oilTax": 0, " + " "classLevel": "Y", " + " "zvItemId": 0, " + " "customerId": 0, " + " "maxPassengerNum": 0, " + " "childOilTax": 0, " + " "childVenderPrice": 620, " + " "sortPrice": 0, " + " "minPassengerNum": 0, " + " "rtDiscount": 0, " + " "fcClassType": "5", " + " "classNoCn": "", " + " "zvItemIdGo": 0, " + " "promotionBeans": 0, " + " "skuId": 0, " + " "agentFeeGo": 0.0, " + " "venderPrice": 1240, " + " "oldDiscount": 0, " + " "promotionPrice": 0.0, " + " "fullPrice": 1240, " + " "discountFlag": false, " + " "luggallow": "", " + " "childPrice": 0, " + " "avItemTax": 0, " + " "deficitPrice": 1240, " + " "resetPolicy": false, " + " "uniqueKey": "v/UmAGQoE1+A3k+c8S1V75wUyW4oMfSQ9Rp/VJ4NrDTuaxFl82q79w==", " + " "sourceId": "661726", " + " "refundFeeFormulaId": 0, " + " "classNo": "Y", " + " "pat": false, " + " "iOilTax": 0, " + " "seatNum": "A", " + " "oldprice": 0, " + " "productCode": "", " + " "netPrice": 0, " + " "childSalePrice": 0, " + " "reducePrice": 0, " + " "exsitsActivity": false " + " }], " + " "airwaysCn": "深圳航空", " + " "minPrice": 0, " + " "discountCN": "0" " + "}";