Gson反序列化生成NULL值

时间:2017-01-27 08:04:34

标签: java arrays json arraylist gson

我试图读取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;
    }
}

2 个答案:

答案 0 :(得分:2)

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

"idmessage":"27301"

private String IdMessage

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

private String idmessage

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

@SerializedName("idmessage")
private String idMessage;

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

答案 1 :(得分:0)

你可以沿着这些方向使用某些东西。请注意,您不必使用Files.readAllBytes,但在您的代码中,BufferedReader也可能导致错误。如果您需要ShipDetails作为数组而不是列表,请在TypeToken中对其进行转换或使用您需要的类型。

final GsonBuilder builder = new GsonBuilder();
final Gson gson = builder.enableComplexMapKeySerialization().create();

// you can use the buffered reader here too, but this is easier to debug if shipDetails is a file
final String jsonRepresentation = new String(Files.readAllBytes(shipDetails);

// add a type definition
final Type type = new TypeToken<ArrayList<ShipDetail>>(){}.getType();
ArrayList<ShipDetail> shipDetails = gson.fromJson(jsonRepresentation, type);

正如上面的评论所述,生成Getters和Setters。