我正在尝试使用Gson将此测试json字符串加载到对象中,但每个元素都带有null。转换期间没有触发异常。请让我知道我可能做错了什么。 感谢您的帮助。
我在这里写一篇较长的评论只是因为没有写出足够的文字来询问我的查询而引发错误。我无法想象为什么会这样做。
String testSeg1 = "{\"FlightSegment\":{\"discount\":0,\"childTaxBreakup\":\"0,0,0\",\"adultTaxBreakup\":\"0,154,413\",\"viaFlight\":\"\",\"airportTaxInfant\":0,\"OperatingAirlineFlightNumber\":144,\"StopQuantity\":0,\"BookingClass\":{\"Availability\":9,\"ResBookDesigCode\":\"S\"},\"AirEquipType\":\"77W\",\"RPH\":\"\",\"airportTax\":567,\"airportTaxChild\":0,\"OperatingAirlineCode\":\"AI\",\"DepartureDateTime\":\"2017-11-27T17:00:00\",\"ArrivalDateTime\":\"2017-11-27T19:10:00\",\"imageFileName\":\"http://live.arzoo.com/FlightWS/image/AirIndia.gif\",\"FlightNumber\":144,\"airLineName\":\"Air India\",\"DepartureAirportCode\":\"BOM\",\"octax\":0,\"ArrivalAirportCode\":\"DEL\",\"BookingClassFare\":{\"bookingclass\":\"S\",\"adultFare\":3955,\"commissionOnTCharge\":0,\"farebasiscode\":\"glW5J3cLgtM=\",\"Rule\":\"This fare is Non Refundable <br> Baggage : 25K<br>Booking Class : S|Re-Schedule Charges: Rs. 750 per sector + Fare difference (If any) +admin fee 500 + Service Fee of Rs. 250 Sector .|Cancellation Charges : Basic fare +Airline administration fee 500 + Service Charges 250 Per Passenger Per Sector .\\t\\t\\t\\t\\t |\",\"adultCommission\":0,\"childCommission\":0,\"classType\":\"Economy\"},\"infantTaxBreakup\":\"0,0,0\"}}";
Type testlistType = new TypeToken<FlightSegment>(){}.getType();
FlightSegment testFlt = gson.fromJson(testSeg1, testlistType);
public class FlightSegment {
@SerializedName("Discount")
@Expose
private String discount;
@SerializedName("AirEquipType")
@Expose
private String airEquipType;
@SerializedName("ArrivalAirportCode")
@Expose
private String arrivalAirportCode;
@SerializedName("ArrivalDateTime")
@Expose
private String arrivalDateTime;
@SerializedName("DepartureAirportCode")
@Expose
private String departureAirportCode;
@SerializedName("DepartureDateTime")
@Expose
private String departureDateTime;
@SerializedName("FlightNumber")
@Expose
private String flightNumber;
@SerializedName("OperatingAirlineCode")
@Expose
private String operatingAirlineCode;
@SerializedName("OperatingAirlineFlightNumber")
@Expose
private String operatingAirlineFlightNumber;
@SerializedName("RPH")
@Expose
private String rPH;
@SerializedName("StopQuantity")
@Expose
private String stopQuantity;
@SerializedName("airLineName")
@Expose
private String airLineName;
@SerializedName("airportTax")
@Expose
private String airportTax;
@SerializedName("imageFileName")
@Expose
private String imageFileName;
@SerializedName("viaFlight")
@Expose
private String viaFlight;
@SerializedName("airportTaxChild")
@Expose
private String airportTaxChild;
@SerializedName("airportTaxInfant")
@Expose
private String airportTaxInfant;
@SerializedName("adultTaxBreakup")
@Expose
private String adultTaxBreakup;
@SerializedName("childTaxBreakup")
@Expose
private String childTaxBreakup;
@SerializedName("infantTaxBreakup")
@Expose
private String infantTaxBreakup;
@SerializedName("octax")
@Expose
private String octax;
@SerializedName("BookingClass")
@Expose
private BookingClass bookingClass;
@SerializedName("BookingClassFare")
@Expose
private BookingClassFare bookingClassFare;
public String getAirEquipType() {
return airEquipType;
}
public void setAirEquipType(String airEquipType) {
this.airEquipType = airEquipType;
}
public String getArrivalAirportCode() {
return arrivalAirportCode;
}
public void setArrivalAirportCode(String arrivalAirportCode) {
this.arrivalAirportCode = arrivalAirportCode;
}
public String getArrivalDateTime() {
return arrivalDateTime;
}
public void setArrivalDateTime(String arrivalDateTime) {
this.arrivalDateTime = arrivalDateTime;
}
public String getDepartureAirportCode() {
return departureAirportCode;
}
public void setDepartureAirportCode(String departureAirportCode) {
this.departureAirportCode = departureAirportCode;
}
public String getDepartureDateTime() {
return departureDateTime;
}
public void setDepartureDateTime(String departureDateTime) {
this.departureDateTime = departureDateTime;
}
public String getFlightNumber() {
return flightNumber;
}
public void setFlightNumber(String flightNumber) {
this.flightNumber = flightNumber;
}
public String getOperatingAirlineCode() {
return operatingAirlineCode;
}
public void setOperatingAirlineCode(String operatingAirlineCode) {
this.operatingAirlineCode = operatingAirlineCode;
}
public String getOperatingAirlineFlightNumber() {
return operatingAirlineFlightNumber;
}
public void setOperatingAirlineFlightNumber(String operatingAirlineFlightNumber) {
this.operatingAirlineFlightNumber = operatingAirlineFlightNumber;
}
public String getRPH() {
return rPH;
}
public void setRPH(String rPH) {
this.rPH = rPH;
}
public String getStopQuantity() {
return stopQuantity;
}
public void setStopQuantity(String stopQuantity) {
this.stopQuantity = stopQuantity;
}
public String getAirLineName() {
return airLineName;
}
public void setAirLineName(String airLineName) {
this.airLineName = airLineName;
}
public String getAirportTax() {
return airportTax;
}
public void setAirportTax(String airportTax) {
this.airportTax = airportTax;
}
public String getImageFileName() {
return imageFileName;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
public String getViaFlight() {
return viaFlight;
}
public void setViaFlight(String viaFlight) {
this.viaFlight = viaFlight;
}
public BookingClass getBookingClass() {
return bookingClass;
}
public void setBookingClass(BookingClass bookingClass) {
this.bookingClass = bookingClass;
}
public BookingClassFare getBookingClassFare() {
return bookingClassFare;
}
public void setBookingClassFare(BookingClassFare bookingClassFare) {
this.bookingClassFare = bookingClassFare;
}
public String getDiscount() {
return discount;
}
public void setDiscount(String discount) {
this.discount = discount;
}
public String getAirportTaxChild() {
return airportTaxChild;
}
public void setAirportTaxChild(String airportTaxChild) {
this.airportTaxChild = airportTaxChild;
}
public String getAirportTaxInfant() {
return airportTaxInfant;
}
public void setAirportTaxInfant(String airportTaxInfant) {
this.airportTaxInfant = airportTaxInfant;
}
public String getAdultTaxBreakup() {
return adultTaxBreakup;
}
public void setAdultTaxBreakup(String adultTaxBreakup) {
this.adultTaxBreakup = adultTaxBreakup;
}
public String getChildTaxBreakup() {
return childTaxBreakup;
}
public void setChildTaxBreakup(String childTaxBreakup) {
this.childTaxBreakup = childTaxBreakup;
}
public String getInfantTaxBreakup() {
return infantTaxBreakup;
}
public void setInfantTaxBreakup(String infantTaxBreakup) {
this.infantTaxBreakup = infantTaxBreakup;
}
public String getOctax() {
return octax;
}
public void setOctax(String octax) {
this.octax = octax;
}
}
这是JSON字符串 -
{"FlightSegment":{"discount":0,"childTaxBreakup":"0,0,0","adultTaxBreakup":"0,154,413","viaFlight":"","airportTaxInfant":0,"OperatingAirlineFlightNumber":144,"StopQuantity":0,"BookingClass":{"Availability":9,"ResBookDesigCode":"S"},"AirEquipType":"77W","RPH":"","airportTax":567,"airportTaxChild":0,"OperatingAirlineCode":"AI","DepartureDateTime":"2017-11-27T17:00:00","ArrivalDateTime":"2017-11-27T19:10:00","imageFileName":"http://live.arzoo.com/FlightWS/image/AirIndia.gif","FlightNumber":144,"airLineName":"Air India","DepartureAirportCode":"BOM","octax":0,"ArrivalAirportCode":"DEL","BookingClassFare":{"bookingclass":"S","adultFare":3955,"commissionOnTCharge":0,"farebasiscode":"glW5J3cLgtM=","Rule":"This fare is Non Refundable <br> Baggage : 25K<br>Booking Class : S|Re-Schedule Charges: Rs. 750 per sector + Fare difference (If any) +admin fee 500 + Service Fee of Rs. 250 Sector .|Cancellation Charges : Basic fare +Airline administration fee 500 + Service Charges 250 Per Passenger Per Sector .ttttt |","adultCommission":0,"childCommission":0,"classType":"Economy"},"infantTaxBreakup":"0,0,0"}}
答案 0 :(得分:0)
请勿使用TypeToken,因为您没有加载对象列表,请直接使用该类:
FlightSegment testFlt = gson.fromJson(testSeg1, FlightSegment.class);