java - JSON/GSON 解析返回某些属性的空值

标签 java json gson

<分区>

我一直在尝试使用 GSON 解析 JSON 文件以显示内容。对于诸如 ship name 之类的属性,我得到了适当的结果,但对于许多其他属性,例如 typeOfShipCargoAISversion 等,我不断得到 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
}

系统类

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());
        }
}

最佳答案

这些字段的名称与 Java 类中的字段名称(或其相应的 getter 和 setter)不匹配。您可以使用 SerializedName为字段使用非默认名称的注释:

 @SerializedName("type_of_ship_and_cargo")
 //@SerializedName(alternate = "type_of_ship_and_cargo")
 private String typeOfShipCargo;

有时,注释的 alternate 部分足以使反序列化变得灵活,但使用默认命名进行序列化。

关于java - JSON/GSON 解析返回某些属性的空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41917697/

相关文章:

java - 如果终端不执行任何操作,如何运行 java 类?

java - 为什么使用 apache-email 库时附件文件名损坏?

java - 如何从原始消息字符串中选取 json 对象?

javascript - 将项目保存为增量 json 差异?

javascript - 如何访问名为日期的对象参数

java - 如何从另一个任务中取消 ScheduledFuture 任务并优雅地结束?

iphone - 如何将数据从一个实体传递到另一个实体类?

java - GSON 应为 Begin_object 但名称为 JSON

java - gson - 如何比较 JsonObjects 忽略几个节点?

java - 我如何查看从 GSON JsonReader 收到了多少字节