1
GSONを使用してJSONファイルを解析してコンテンツを表示しようとしています。 ship name
のような属性の場合、私は適切な結果を得ていますが、typeOfShipCargo
、AISversion
などの多くのものについては、私はnull
の値を得続けます。JSON/GSON解析で特定の属性のnull値が返される
私は以下のコードの大部分を与えました。他に何かを追加する必要があるかどうかを教えてください。乾杯。
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": "Test",
"destination": "",
"last_static_draught": "0",
"DTE": "127"
}
]
親クラス
public class Ship {
private String idmsg;
private String idsession;
private String systemTime;
private String NMEAstring;
private String processed;
private String MMSI;
public Ship(){}
public Ship(String idmsg, String idsession, String systemTime, String NMEAstring, String processed, String MMSI) {
this.idmsg = idmsg;
this.idsession = idsession;
this.systemTime = systemTime;
this.NMEAstring = NMEAstring;
this.processed = processed;
this.MMSI = MMSI;
}
//Getters and setters
}
子クラス
public class ShipDetails extends Ship {
private String AISversion;
private String IMOnumber;
private String callSign;
private String name;
private String typeOfShipCargo;
private String bowToPositionUnit;
private String sternToPositionUnit;
private String portToPositionUnit;
private String starboardToPositionUnit;
private String typeOfPositionFixingDevice;
private String eta;
private String destination;
private String lastStaticDraught;
private String dte;
public ShipDetails(String idmsg, String idsession, String systemTime, String NMEAstring, String processed,
String MMSI, String AISversion, String IMOnumber, String callSign, String name, The rest...) {
super(idmsg, idsession, systemTime, NMEAstring, processed, MMSI);
this.AISversion = AISversion;
this.IMOnumber = IMOnumber;
this.callSign = callSign;
this.name = name;
this.typeOfShipCargo = typeOfShipCargo;
this.bowToPositionUnit = bowToPositionUnit;
this.sternToPositionUnit = sternToPositionUnit;
this.portToPositionUnit = portToPositionUnit;
this.starboardToPositionUnit = starboardToPositionUnit;
this.typeOfPositionFixingDevice = typeOfPositionFixingDevice;
this.eta = eta;
this.destination = destination;
this.lastStaticDraught = lastStaticDraught;
this.dte = dte;
}
//getters and setters
}
Systemクラス
public class ShipController {
private static ArrayList<ShipDetails> shipDet = new ArrayList<ShipDetails>();
public static void main(String[] args) throws IOException{
File inStream = new File("details.json");
Scanner read = new Scanner(inStream);
BufferedReader bf = new BufferedReader(new FileReader("positions.json"));
String json = "";
while (read.hasNext())
{
json += read.nextLine();
}
Gson gson = new Gson();
ShipDetails[] tempShip = gson.fromJson(json, ShipDetails[].class);
if(tempShip == null)
System.out.println("Null");
shipDet.addAll(Arrays.asList(tempShip));
shipPos.addAll(Arrays.asList(tempDetails));
read.close();
//Printing the attribute values. For AIS version it retuns null while for name it doesn't.
for (ShipDetails d : shipDet)
{
System.out.println(d.getAISversion());
}
}
Goddamnit私は自分のコードで何が間違っているかを調べるために頭を悩ましてきました。本当にありがとう! – ZeroDarkThirty