Java中Json字符串转换为对象的方法(多层List集合)
借鉴自: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 = "{\n" + "\t\"oldminPrice\": 0,\n" + "\t\"depTerminal\": \"T3\",\n" + "\t\"minDiscount\": 0,\n" + "\t\"earningLevel\": 0,\n" + "\t\"asr\": false,\n" + "\t\"sourceId\": \"661726\",\n" + "\t\"arrCity\": \"HFE\",\n" + "\t\"childfee\": 0,\n" + "\t\"flightNo\": \"ZH9899\",\n" + "\t\"planeStyle\": \"320\",\n" + "\t\"arrTime\": \"0025\",\n" + "\t\"codeShare\": false,\n" + "\t\"arrDate\": \"2018-02-05\",\n" + "\t\"depTime\": \"2215\",\n" + "\t\"depDate\": \"2018-02-04\",\n" + "\t\"isStop\": \"0\",\n" + "\t\"airways\": \"ZH\",\n" + "\t\"depAirdrome\": \"宝安机场\",\n" + "\t\"depCity\": \"SZX\",\n" + "\t\"exsitsActivity\": false,\n" + "\t\"fee\": 0,\n" + "\t\"tax\": 50,\n" + "\t\"arrAirdrome\": \"新桥机场\",\n" + "\t\"originalMinPrice\": 0,\n" + "\t\"arrTerminal\": \"机场\",\n" + "\t\"fullPrice\": 1240,\n" + "\t\"bingoClassInfoList\": [{\n" + "\t\t\"airCompanyFlag\": true,\n" + "\t\t\"activityId\": 0,\n" + "\t\t\"infantVenderPrice\": 0,\n" + "\t\t\"policyId\": \"v/UmAGQoE1+A3k+c8S1V75wUyW4oMfSQSp/WJesbrOPuaxFl82q79w==\",\n" + "\t\t\"rfId\": 0,\n" + "\t\t\"netChildPrice\": 0,\n" + "\t\t\"agentFee\": 0.0,\n" + "\t\t\"promotionReducePrice\": 0,\n" + "\t\t\"fareItemId\": \"jdselfbspZHZH98992018-02-04F\",\n" + "\t\t\"discount\": 250,\n" + "\t\t\"fee\": 0,\n" + "\t\t\"subsidy\": 0,\n" + "\t\t\"childSeatCode\": \"F\",\n" + "\t\t\"price\": 0,\n" + "\t\t\"originalPrice\": 3100,\n" + "\t\t\"infantSalePrice\": 0,\n" + "\t\t\"newMinPrice\": 0,\n" + "\t\t\"oilTax\": 0,\n" + "\t\t\"classLevel\": \"F\",\n" + "\t\t\"zvItemId\": 0,\n" + "\t\t\"customerId\": 0,\n" + "\t\t\"maxPassengerNum\": 0,\n" + "\t\t\"childOilTax\": 0,\n" + "\t\t\"childVenderPrice\": 1550,\n" + "\t\t\"sortPrice\": 0,\n" + "\t\t\"minPassengerNum\": 0,\n" + "\t\t\"rtDiscount\": 0,\n" + "\t\t\"fcClassType\": \"5\",\n" + "\t\t\"classNoCn\": \"\",\n" + "\t\t\"zvItemIdGo\": 0,\n" + "\t\t\"promotionBeans\": 0,\n" + "\t\t\"skuId\": 0,\n" + "\t\t\"agentFeeGo\": 0.0,\n" + "\t\t\"venderPrice\": 3100,\n" + "\t\t\"oldDiscount\": 0,\n" + "\t\t\"promotionPrice\": 0.0,\n" + "\t\t\"fullPrice\": 1240,\n" + "\t\t\"discountFlag\": false,\n" + "\t\t\"luggallow\": \"\",\n" + "\t\t\"childPrice\": 0,\n" + "\t\t\"avItemTax\": 0,\n" + "\t\t\"deficitPrice\": 3100,\n" + "\t\t\"resetPolicy\": false,\n" + "\t\t\"uniqueKey\": \"v/UmAGQoE1+A3k+c8S1V75wUyW4oMfSQSp/WJesbrOPuaxFl82q79w==\",\n" + "\t\t\"sourceId\": \"661726\",\n" + "\t\t\"refundFeeFormulaId\": 0,\n" + "\t\t\"classNo\": \"F\",\n" + "\t\t\"pat\": false,\n" + "\t\t\"iOilTax\": 0,\n" + "\t\t\"seatNum\": \"6\",\n" + "\t\t\"oldprice\": 0,\n" + "\t\t\"productCode\": \"\",\n" + "\t\t\"netPrice\": 0,\n" + "\t\t\"childSalePrice\": 0,\n" + "\t\t\"reducePrice\": 0,\n" + "\t\t\"exsitsActivity\": false\n" + "\t}, {\n" + "\t\t\"airCompanyFlag\": true,\n" + "\t\t\"activityId\": 0,\n" + "\t\t\"infantVenderPrice\": 0,\n" + "\t\t\"policyId\": \"v/UmAGQoE1+A3k+c8S1V75wUyW4oMfSQL5O7RthR+czuaxFl82q79w==\",\n" + "\t\t\"rfId\": 0,\n" + "\t\t\"netChildPrice\": 0,\n" + "\t\t\"agentFee\": 0.0,\n" + "\t\t\"promotionReducePrice\": 0,\n" + "\t\t\"fareItemId\": \"jdselfbspZHZH98992018-02-04C\",\n" + "\t\t\"discount\": 240,\n" + "\t\t\"fee\": 0,\n" + "\t\t\"subsidy\": 0,\n" + "\t\t\"childSeatCode\": \"C\",\n" + "\t\t\"price\": 0,\n" + "\t\t\"originalPrice\": 2980,\n" + "\t\t\"infantSalePrice\": 0,\n" + "\t\t\"newMinPrice\": 0,\n" + "\t\t\"oilTax\": 0,\n" + "\t\t\"classLevel\": \"C\",\n" + "\t\t\"zvItemId\": 0,\n" + "\t\t\"customerId\": 0,\n" + "\t\t\"maxPassengerNum\": 0,\n" + "\t\t\"childOilTax\": 0,\n" + "\t\t\"childVenderPrice\": 1490,\n" + "\t\t\"sortPrice\": 0,\n" + "\t\t\"minPassengerNum\": 0,\n" + "\t\t\"rtDiscount\": 0,\n" + "\t\t\"fcClassType\": \"5\",\n" + "\t\t\"classNoCn\": \"\",\n" + "\t\t\"zvItemIdGo\": 0,\n" + "\t\t\"promotionBeans\": 0,\n" + "\t\t\"skuId\": 0,\n" + "\t\t\"agentFeeGo\": 0.0,\n" + "\t\t\"venderPrice\": 2980,\n" + "\t\t\"oldDiscount\": 0,\n" + "\t\t\"promotionPrice\": 0.0,\n" + "\t\t\"fullPrice\": 1240,\n" + "\t\t\"discountFlag\": false,\n" + "\t\t\"luggallow\": \"\",\n" + "\t\t\"childPrice\": 0,\n" + "\t\t\"avItemTax\": 0,\n" + "\t\t\"deficitPrice\": 2980,\n" + "\t\t\"resetPolicy\": false,\n" + "\t\t\"uniqueKey\": \"v/UmAGQoE1+A3k+c8S1V75wUyW4oMfSQL5O7RthR+czuaxFl82q79w==\",\n" + "\t\t\"sourceId\": \"661726\",\n" + "\t\t\"refundFeeFormulaId\": 0,\n" + "\t\t\"classNo\": \"C\",\n" + "\t\t\"pat\": false,\n" + "\t\t\"iOilTax\": 0,\n" + "\t\t\"seatNum\": \"2\",\n" + "\t\t\"oldprice\": 0,\n" + "\t\t\"productCode\": \"\",\n" + "\t\t\"netPrice\": 0,\n" + "\t\t\"childSalePrice\": 0,\n" + "\t\t\"reducePrice\": 0,\n" + "\t\t\"exsitsActivity\": false\n" + "\t}, {\n" + "\t\t\"airCompanyFlag\": true,\n" + "\t\t\"activityId\": 0,\n" + "\t\t\"infantVenderPrice\": 0,\n" + "\t\t\"policyId\": \"v/UmAGQoE1+A3k+c8S1V75wUyW4oMfSQ0UENkdxWb2buaxFl82q79w==\",\n" + "\t\t\"rfId\": 0,\n" + "\t\t\"netChildPrice\": 0,\n" + "\t\t\"agentFee\": 0.0,\n" + "\t\t\"promotionReducePrice\": 0,\n" + "\t\t\"fareItemId\": \"jdselfbspZHZH98992018-02-04M\",\n" + "\t\t\"discount\": 96,\n" + "\t\t\"fee\": 0,\n" + "\t\t\"subsidy\": 0,\n" + "\t\t\"childSeatCode\": \"Y\",\n" + "\t\t\"price\": 0,\n" + "\t\t\"originalPrice\": 1190,\n" + "\t\t\"infantSalePrice\": 0,\n" + "\t\t\"newMinPrice\": 0,\n" + "\t\t\"oilTax\": 0,\n" + "\t\t\"classLevel\": \"Y\",\n" + "\t\t\"zvItemId\": 0,\n" + "\t\t\"customerId\": 0,\n" + "\t\t\"maxPassengerNum\": 0,\n" + "\t\t\"childOilTax\": 0,\n" + "\t\t\"childVenderPrice\": 620,\n" + "\t\t\"sortPrice\": 0,\n" + "\t\t\"minPassengerNum\": 0,\n" + "\t\t\"rtDiscount\": 0,\n" + "\t\t\"fcClassType\": \"5\",\n" + "\t\t\"classNoCn\": \"\",\n" + "\t\t\"zvItemIdGo\": 0,\n" + "\t\t\"promotionBeans\": 0,\n" + "\t\t\"skuId\": 0,\n" + "\t\t\"agentFeeGo\": 0.0,\n" + "\t\t\"venderPrice\": 1190,\n" + "\t\t\"oldDiscount\": 0,\n" + "\t\t\"promotionPrice\": 0.0,\n" + "\t\t\"fullPrice\": 1240,\n" + "\t\t\"discountFlag\": false,\n" + "\t\t\"luggallow\": \"\",\n" + "\t\t\"childPrice\": 0,\n" + "\t\t\"avItemTax\": 0,\n" + "\t\t\"deficitPrice\": 1190,\n" + "\t\t\"resetPolicy\": false,\n" + "\t\t\"uniqueKey\": \"v/UmAGQoE1+A3k+c8S1V75wUyW4oMfSQ0UENkdxWb2buaxFl82q79w==\",\n" + "\t\t\"sourceId\": \"661726\",\n" + "\t\t\"refundFeeFormulaId\": 0,\n" + "\t\t\"classNo\": \"M\",\n" + "\t\t\"pat\": false,\n" + "\t\t\"iOilTax\": 0,\n" + "\t\t\"seatNum\": \"A\",\n" + "\t\t\"oldprice\": 0,\n" + "\t\t\"productCode\": \"\",\n" + "\t\t\"netPrice\": 0,\n" + "\t\t\"childSalePrice\": 0,\n" + "\t\t\"reducePrice\": 0,\n" + "\t\t\"exsitsActivity\": false\n" + "\t}, {\n" + "\t\t\"airCompanyFlag\": true,\n" + "\t\t\"activityId\": 0,\n" + "\t\t\"infantVenderPrice\": 0,\n" + "\t\t\"policyId\": \"v/UmAGQoE1+A3k+c8S1V75wUyW4oMfSQpv8z2L3W34nuaxFl82q79w==\",\n" + "\t\t\"rfId\": 0,\n" + "\t\t\"netChildPrice\": 0,\n" + "\t\t\"agentFee\": 0.0,\n" + "\t\t\"promotionReducePrice\": 0,\n" + "\t\t\"fareItemId\": \"jdselfbspZHZH98992018-02-04H\",\n" + "\t\t\"discount\": 81,\n" + "\t\t\"fee\": 0,\n" + "\t\t\"subsidy\": 0,\n" + "\t\t\"childSeatCode\": \"Y\",\n" + "\t\t\"price\": 0,\n" + "\t\t\"originalPrice\": 1000,\n" + "\t\t\"infantSalePrice\": 0,\n" + "\t\t\"newMinPrice\": 0,\n" + "\t\t\"oilTax\": 0,\n" + "\t\t\"classLevel\": \"Y\",\n" + "\t\t\"zvItemId\": 0,\n" + "\t\t\"customerId\": 0,\n" + "\t\t\"maxPassengerNum\": 0,\n" + "\t\t\"childOilTax\": 0,\n" + "\t\t\"childVenderPrice\": 620,\n" + "\t\t\"sortPrice\": 0,\n" + "\t\t\"minPassengerNum\": 0,\n" + "\t\t\"rtDiscount\": 0,\n" + "\t\t\"fcClassType\": \"5\",\n" + "\t\t\"classNoCn\": \"\",\n" + "\t\t\"zvItemIdGo\": 0,\n" + "\t\t\"promotionBeans\": 0,\n" + "\t\t\"skuId\": 0,\n" + "\t\t\"agentFeeGo\": 0.0,\n" + "\t\t\"venderPrice\": 1000,\n" + "\t\t\"oldDiscount\": 0,\n" + "\t\t\"promotionPrice\": 0.0,\n" + "\t\t\"fullPrice\": 1240,\n" + "\t\t\"discountFlag\": false,\n" + "\t\t\"luggallow\": \"\",\n" + "\t\t\"childPrice\": 0,\n" + "\t\t\"avItemTax\": 0,\n" + "\t\t\"deficitPrice\": 1000,\n" + "\t\t\"resetPolicy\": false,\n" + "\t\t\"uniqueKey\": \"v/UmAGQoE1+A3k+c8S1V75wUyW4oMfSQpv8z2L3W34nuaxFl82q79w==\",\n" + "\t\t\"sourceId\": \"661726\",\n" + "\t\t\"refundFeeFormulaId\": 0,\n" + "\t\t\"classNo\": \"H\",\n" + "\t\t\"pat\": false,\n" + "\t\t\"iOilTax\": 0,\n" + "\t\t\"seatNum\": \"A\",\n" + "\t\t\"oldprice\": 0,\n" + "\t\t\"productCode\": \"\",\n" + "\t\t\"netPrice\": 0,\n" + "\t\t\"childSalePrice\": 0,\n" + "\t\t\"reducePrice\": 0,\n" + "\t\t\"exsitsActivity\": false\n" + "\t}, {\n" + "\t\t\"airCompanyFlag\": true,\n" + "\t\t\"activityId\": 0,\n" + "\t\t\"infantVenderPrice\": 0,\n" + "\t\t\"policyId\": \"v/UmAGQoE1+A3k+c8S1V75wUyW4oMfSQy40pO0Np5b/uaxFl82q79w==\",\n" + "\t\t\"rfId\": 0,\n" + "\t\t\"netChildPrice\": 0,\n" + "\t\t\"agentFee\": 0.0,\n" + "\t\t\"promotionReducePrice\": 0,\n" + "\t\t\"fareItemId\": \"jdselfbspZHZH98992018-02-04U\",\n" + "\t\t\"discount\": 86,\n" + "\t\t\"fee\": 0,\n" + "\t\t\"subsidy\": 0,\n" + "\t\t\"childSeatCode\": \"Y\",\n" + "\t\t\"price\": 0,\n" + "\t\t\"originalPrice\": 1070,\n" + "\t\t\"infantSalePrice\": 0,\n" + "\t\t\"newMinPrice\": 0,\n" + "\t\t\"oilTax\": 0,\n" + "\t\t\"classLevel\": \"Y\",\n" + "\t\t\"zvItemId\": 0,\n" + "\t\t\"customerId\": 0,\n" + "\t\t\"maxPassengerNum\": 0,\n" + "\t\t\"childOilTax\": 0,\n" + "\t\t\"childVenderPrice\": 620,\n" + "\t\t\"sortPrice\": 0,\n" + "\t\t\"minPassengerNum\": 0,\n" + "\t\t\"rtDiscount\": 0,\n" + "\t\t\"fcClassType\": \"5\",\n" + "\t\t\"classNoCn\": \"\",\n" + "\t\t\"zvItemIdGo\": 0,\n" + "\t\t\"promotionBeans\": 0,\n" + "\t\t\"skuId\": 0,\n" + "\t\t\"agentFeeGo\": 0.0,\n" + "\t\t\"venderPrice\": 1070,\n" + "\t\t\"oldDiscount\": 0,\n" + "\t\t\"promotionPrice\": 0.0,\n" + "\t\t\"fullPrice\": 1240,\n" + "\t\t\"discountFlag\": false,\n" + "\t\t\"luggallow\": \"\",\n" + "\t\t\"childPrice\": 0,\n" + "\t\t\"avItemTax\": 0,\n" + "\t\t\"deficitPrice\": 1070,\n" + "\t\t\"resetPolicy\": false,\n" + "\t\t\"uniqueKey\": \"v/UmAGQoE1+A3k+c8S1V75wUyW4oMfSQy40pO0Np5b/uaxFl82q79w==\",\n" + "\t\t\"sourceId\": \"661726\",\n" + "\t\t\"refundFeeFormulaId\": 0,\n" + "\t\t\"classNo\": \"U\",\n" + "\t\t\"pat\": false,\n" + "\t\t\"iOilTax\": 0,\n" + "\t\t\"seatNum\": \"A\",\n" + "\t\t\"oldprice\": 0,\n" + "\t\t\"productCode\": \"\",\n" + "\t\t\"netPrice\": 0,\n" + "\t\t\"childSalePrice\": 0,\n" + "\t\t\"reducePrice\": 0,\n" + "\t\t\"exsitsActivity\": false\n" + "\t}, {\n" + "\t\t\"airCompanyFlag\": true,\n" + "\t\t\"activityId\": 0,\n" + "\t\t\"infantVenderPrice\": 0,\n" + "\t\t\"policyId\": \"v/UmAGQoE1+A3k+c8S1V75wUyW4oMfSQ9Rp/VJ4NrDTuaxFl82q79w==\",\n" + "\t\t\"rfId\": 0,\n" + "\t\t\"netChildPrice\": 0,\n" + "\t\t\"agentFee\": 0.0,\n" + "\t\t\"promotionReducePrice\": 0,\n" + "\t\t\"fareItemId\": \"jdselfbspZHZH98992018-02-04Y\",\n" + "\t\t\"discount\": 100,\n" + "\t\t\"fee\": 0,\n" + "\t\t\"subsidy\": 0,\n" + "\t\t\"childSeatCode\": \"Y\",\n" + "\t\t\"price\": 0,\n" + "\t\t\"originalPrice\": 1240,\n" + "\t\t\"infantSalePrice\": 0,\n" + "\t\t\"newMinPrice\": 0,\n" + "\t\t\"oilTax\": 0,\n" + "\t\t\"classLevel\": \"Y\",\n" + "\t\t\"zvItemId\": 0,\n" + "\t\t\"customerId\": 0,\n" + "\t\t\"maxPassengerNum\": 0,\n" + "\t\t\"childOilTax\": 0,\n" + "\t\t\"childVenderPrice\": 620,\n" + "\t\t\"sortPrice\": 0,\n" + "\t\t\"minPassengerNum\": 0,\n" + "\t\t\"rtDiscount\": 0,\n" + "\t\t\"fcClassType\": \"5\",\n" + "\t\t\"classNoCn\": \"\",\n" + "\t\t\"zvItemIdGo\": 0,\n" + "\t\t\"promotionBeans\": 0,\n" + "\t\t\"skuId\": 0,\n" + "\t\t\"agentFeeGo\": 0.0,\n" + "\t\t\"venderPrice\": 1240,\n" + "\t\t\"oldDiscount\": 0,\n" + "\t\t\"promotionPrice\": 0.0,\n" + "\t\t\"fullPrice\": 1240,\n" + "\t\t\"discountFlag\": false,\n" + "\t\t\"luggallow\": \"\",\n" + "\t\t\"childPrice\": 0,\n" + "\t\t\"avItemTax\": 0,\n" + "\t\t\"deficitPrice\": 1240,\n" + "\t\t\"resetPolicy\": false,\n" + "\t\t\"uniqueKey\": \"v/UmAGQoE1+A3k+c8S1V75wUyW4oMfSQ9Rp/VJ4NrDTuaxFl82q79w==\",\n" + "\t\t\"sourceId\": \"661726\",\n" + "\t\t\"refundFeeFormulaId\": 0,\n" + "\t\t\"classNo\": \"Y\",\n" + "\t\t\"pat\": false,\n" + "\t\t\"iOilTax\": 0,\n" + "\t\t\"seatNum\": \"A\",\n" + "\t\t\"oldprice\": 0,\n" + "\t\t\"productCode\": \"\",\n" + "\t\t\"netPrice\": 0,\n" + "\t\t\"childSalePrice\": 0,\n" + "\t\t\"reducePrice\": 0,\n" + "\t\t\"exsitsActivity\": false\n" + "\t}],\n" + "\t\"airwaysCn\": \"深圳航空\",\n" + "\t\"minPrice\": 0,\n" + "\t\"discountCN\": \"0\"\n" + "}";