使用继承读取 Json
Reading Json with Inheritance
我正在尝试读取 Java 中的 Json 文件。但是我不知道如何将包含 Java 文件的数据分发给子类。
我有一个超类,然后我有三个子类,这取决于我必须填写或不填写的数据,我不知道如何根据提供的文件的数据填充这三个子类(扩展超类)。
这里有一个示例,说明如何做到这一点 achieved.But 请记住要这样做,您需要在 json 中提供类型信息,以便它使用类型信息进行转换json 到 Java 对象。
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")
@JsonSubTypes({
@Type(value = Car.class, name = "car"),
@Type(value = Truck.class, name = "truck")
}) // magic happens here, when we give type information using per-class annotations, this will be used by jackson to convert the json to appropriate java objects.
public abstract class Vehicle {
private String make;
private String model;
protected Vehicle(String make, String model) {
this.make = make;
this.model = model;
}
// no-arg constructor, getters and setters
}
车子class
@JsonIgnoreProperties({ // any properties from parent that can be ignored })
public class Car extends Vehicle {
// any properties from this class to be ignored place it above the property
@JsonIgnore
private int seatingCapacity;
private double topSpeed;
public Car(String make, String model, int seatingCapacity, double topSpeed) {
super(make, model);
this.seatingCapacity = seatingCapacity;
this.topSpeed = topSpeed;
}
// no-arg constructor, getters and setters
}
卡车子class:
public class Truck extends Vehicle {
private double payloadCapacity;
public Truck(String make, String model, double payloadCapacity) {
super(make, model);
this.payloadCapacity = payloadCapacity;
}
// no-arg constructor, getters and setters
}
wrapper class 用于读取 json,它应该与你的 json 数组键相同。
public class VehicleManager {
private List<Vehicle> vehicles;
// setters, getters, no arg constructor
}
主要class
public class AppStart {
public static void main(String[] args) throws FileNotFoundException, IOException {
VehicleManager vehicleManager = new VehicleManager();
ObjectMapper objectMapper = new ObjectMapper();
try{
objectMapper.readerFor(VehicleManager.class).readValue(new File("yourJsonFilePath"));
}
catch(IOException ie){
// log an IOException or do some other operation like rethrow, etc.,
}
catch(Exception e){
// log exception
}
}
}
你的样本 json 应该是
"vehicleManager":[{
"type": "car", // type information should be provided here
"make": "Toyota",
"model": "Camry",
"seatingCapacity": 5,
"topSpeed": 168.8
},
{
"type": "car", // type information should be provided here
"make": "Hyundai",
"model": "Elantra",
"seatingCapacity": 5,
"topSpeed": 178.8
}]
我正在尝试读取 Java 中的 Json 文件。但是我不知道如何将包含 Java 文件的数据分发给子类。 我有一个超类,然后我有三个子类,这取决于我必须填写或不填写的数据,我不知道如何根据提供的文件的数据填充这三个子类(扩展超类)。
这里有一个示例,说明如何做到这一点 achieved.But 请记住要这样做,您需要在 json 中提供类型信息,以便它使用类型信息进行转换json 到 Java 对象。
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")
@JsonSubTypes({
@Type(value = Car.class, name = "car"),
@Type(value = Truck.class, name = "truck")
}) // magic happens here, when we give type information using per-class annotations, this will be used by jackson to convert the json to appropriate java objects.
public abstract class Vehicle {
private String make;
private String model;
protected Vehicle(String make, String model) {
this.make = make;
this.model = model;
}
// no-arg constructor, getters and setters
}
车子class
@JsonIgnoreProperties({ // any properties from parent that can be ignored })
public class Car extends Vehicle {
// any properties from this class to be ignored place it above the property
@JsonIgnore
private int seatingCapacity;
private double topSpeed;
public Car(String make, String model, int seatingCapacity, double topSpeed) {
super(make, model);
this.seatingCapacity = seatingCapacity;
this.topSpeed = topSpeed;
}
// no-arg constructor, getters and setters
}
卡车子class:
public class Truck extends Vehicle {
private double payloadCapacity;
public Truck(String make, String model, double payloadCapacity) {
super(make, model);
this.payloadCapacity = payloadCapacity;
}
// no-arg constructor, getters and setters
}
wrapper class 用于读取 json,它应该与你的 json 数组键相同。
public class VehicleManager {
private List<Vehicle> vehicles;
// setters, getters, no arg constructor
}
主要class
public class AppStart {
public static void main(String[] args) throws FileNotFoundException, IOException {
VehicleManager vehicleManager = new VehicleManager();
ObjectMapper objectMapper = new ObjectMapper();
try{
objectMapper.readerFor(VehicleManager.class).readValue(new File("yourJsonFilePath"));
}
catch(IOException ie){
// log an IOException or do some other operation like rethrow, etc.,
}
catch(Exception e){
// log exception
}
}
}
你的样本 json 应该是
"vehicleManager":[{
"type": "car", // type information should be provided here
"make": "Toyota",
"model": "Camry",
"seatingCapacity": 5,
"topSpeed": 168.8
},
{
"type": "car", // type information should be provided here
"make": "Hyundai",
"model": "Elantra",
"seatingCapacity": 5,
"topSpeed": 178.8
}]