小编典典

Gson反序列化生成NULL值

java

我试图读取JSON文件并将其转换为数组,但是在读取JSON文件后从数组获取空值。我正在为我的ShipDetail班级使用默认构造函数。

  BufferedReader detailReader = new BufferedReader( new  FileReader(shipDetails));
  // Buffered passed to convert json array to java array
  ShipDetail[] shipDetail  = gson.fromJson(detailReader, ShipDetail[].class );

  System.out.println( shipDetail[0].toString());

  // Convert  array to arrayList
  ArrayList<ShipDetail> detailArray = new ArrayList<ShipDetail>(Arrays.asList(shipDetail));

JSON文件:

[  
   {  
      "idmessage":"27301",
      "idsession":"362",
      "time_stamp_system":"2017-01-20 14:51:14",
      "NMEA_string":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
      "processed":"1",
      "MMSI":"0000000001",
      "AIS_version":"0",
      "IMO_number":"xxxxxxxxxx",
      "callSign":"ODLK1",
      "name":"ODLXJ KWW",
      "type_of_ship_and_cargo":"0",
      "bow_to_possition_unit":"212",
      "stern_to_possition_unit":"71",
      "port_to_possition_unit":"22",
      "starboard_to_possitio_unit":"22",
      "type_of_position_fixing_divice":"1",
      "ETA":null,
      "destination":"",
      "last_static_draught":"0",
      "DTE":"127"
   }
]

ShipDetail类:

public class ShipDetail {

    private String IdMessage, IdSession, Time_Stamp_System, Nmea_String, Processed;
    private String Mmsi, Ais_Version, Imo_Number, Callsign, Name, Type_Of_Ship_And_Cargo;
    private String Bow_To_Position_Unit, Stern_To_Position_Unit, Port_To_Position_Unit, Starboard_To_Position_Unit,
            Type_Of_Position_Fixing_Device;
    private String Eta, Destination, Last_Ship_Draught, Dte;

    public String getMmsi() {
        return Mmsi;
    }

    public String toString() {
        return "\n Id Message : " + IdMessage + "\n Id Session : " + IdSession + "\n Time Stam System : "
                + Time_Stamp_System + "\n NMEA String : " + Nmea_String + "\n Processed : " + Processed + "\n MMSI : "
                + Mmsi + "\n AIS Version : " + Ais_Version + "\n IMO Number : " + Imo_Number + "\n Call Sign : "
                + Callsign + "\n Name : " + Name + "\n Type Of Ship And Cargo : " + Type_Of_Ship_And_Cargo
                + "\n Bow To Position Unit : " + Bow_To_Position_Unit + "\n Stern To Position Unit : "
                + Stern_To_Position_Unit + "\n Port To Position Unit : " + Port_To_Position_Unit
                + "\n Starboard To Position Fixing Device : " + Starboard_To_Position_Unit
                + "\n Type Of Position Fixing Device : " + Type_Of_Position_Fixing_Device + "\n ETA : " + Eta
                + "\n Destination : " + Destination + "\n Last Ship Draught : " + Last_Ship_Draught + "\n DTE : " + Dte;
    }
}

阅读 500

收藏
2020-11-26

共1个答案

小编典典

您的Gson映射与给定的JSON不匹配。默认情况下,Gson通过 确切 名称将JSON属性映射到目标映射中的相应字段。看一眼:

"idmessage":"27301"

private String IdMessage

属性名称大小写和字段名称大小写不匹配。您需要的是正确映射JSON。要么:

private String idmessage

或通过覆盖名称匹配(这更适合Java命名约定):

@SerializedName("idmessage")
private String idMessage;

注意每行一个字段。为了分别注释每个字段,这是必需的。或者,如果可能,在Java和JSON中同时使用camelCase。

2020-11-26