Gson 反序列化生成 NULL 值 [英] Gson deserialisation generates NULL value
问题描述
我试图读取 JSON 文件并将其转换为数组,但在读取 JSON 文件后从数组中获取空值.我正在为我的 ShipDetail
类使用默认构造函数.
BufferedReader detailReader = new BufferedReader( new FileReader(shipDetails));//缓冲传递以将 json 数组转换为 java 数组ShipDetail[] shipDetail = gson.fromJson(detailReader, ShipDetail[].class );System.out.println( shipDetail[0].toString());//将数组转换为arrayListArrayListdetailArray = new ArrayList(Arrays.asList(shipDetail));
JSON 文件:
<预><代码>[{"idmessage":"27301","idsession":"362","time_stamp_system":"2017-01-20 14:51:14","NMEA_string":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","已处理":"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",预计到达时间":空,目的地":"","last_static_draught":"0","DTE":"127"}]ShipDetail
类:
public class ShipDetail {私有字符串 IdMessage、IdSession、Time_Stamp_System、Nmea_String、已处理;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;私人字符串 Eta、目的地、Last_Ship_Draught、Dte;公共字符串 getMmsi() {返回 Mmsi;}公共字符串 toString() {return "\n Id Message : " + IdMessage + "\n Id Session :" + IdSession + "\n 时间戳系统:"+ Time_Stamp_System + "\n NMEA 字符串:" + Nmea_String + "\n 已处理:" + 已处理 + "\n MMSI:"+ Mmsi + "\n AIS 版本:" + Ais_Version + "\n IMO 编号:" + Imo_Number + "\n 呼号:"+ 呼号 + "\n 名称:" + 名称 + "\n 船舶和货物类型:" + Type_Of_Ship_And_Cargo+ "\n 船头到位置单位:" + Bow_To_Position_Unit + "\n 船尾到位置单位:"+ Stern_To_Position_Unit + "\n 端口到位置单元:" + Port_To_Position_Unit+ "\n 右舷至位置固定装置:" + Starboard_To_Position_Unit+ "\n 定位装置类型:" + Type_Of_Position_Fixing_Device + "\n ETA :" + Eta+ "\n 目的地:" + 目的地 + "\n 最后船舶吃水:" + Last_Ship_Draught + "\n DTE :" + Dte;}}
您的 Gson 映射与给定的 JSON 不匹配.默认情况下,Gson 通过精确名称将 JSON 属性映射到目标映射中的相应字段.看看:
"idmessage":"27301"
和
private String IdMessage
属性名称大小写和字段名称大小写不匹配.您需要的是正确映射您的 JSON.要么:
private String idmessage
或者通过覆盖名称匹配(这更适合 Java 命名约定):
@SerializedName("idmessage")私人字符串idMessage;
注意每行一个字段.为了分别注释每个字段,这是必需的.或者,如果可能,在 Java 和 JSON 中都使用驼峰式大小写.
I was trying to read and convert a JSON file to an array but getting null values from the array after reading the JSON file. I am using the default constructor for my ShipDetail
class.
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));
The JSON file:
[
{
"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"
}
]
The ShipDetail
class:
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;
}
}
Your Gson mapping does not match the given JSON. By default, Gson maps JSON properties to their appropriate fields in the target mapping by exact name. Take a look at:
"idmessage":"27301"
and
private String IdMessage
The property name case and the field name case do not match. What you need is map your JSON correctly. Either:
private String idmessage
or by overriding the name match (and that's more appropriate for the Java naming conventions):
@SerializedName("idmessage")
private String idMessage;
Note one field per line. This is required in order to annotated each field separately. Or, if possible, use camelCase both in Java and JSON.
这篇关于Gson 反序列化生成 NULL 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!