首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >继承Json的解读

继承Json的解读
EN

Stack Overflow用户
提问于 2019-12-27 16:20:53
回答 1查看 2K关注 0票数 0

我正在尝试用Java读取Json文件。但我不知道如何将包含Java文件的数据分发给子类。我有一个超类,然后有三个子类,这取决于所提供的数据是否需要填充,而且我不知道如何填充这三个子类(扩展了超类),这取决于所提供的文件的数据。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-27 17:18:05

下面是一个示例,说明如何使用achieved.But --请记住--要这样做,您需要在json中提供类型信息,以便使用类型信息将json转换为Java。

代码语言:javascript
复制
@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
}

Car子类

代码语言:javascript
复制
@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
}

卡车子类:

代码语言:javascript
复制
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
}

用于读取json的包装类,它应该与json数组键相同。

代码语言:javascript
复制
public class VehicleManager {
    private List<Vehicle> vehicles;

   // setters, getters, no arg constructor
}

主班

代码语言:javascript
复制
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
    }

}
}

你的样本应该是

代码语言:javascript
复制
"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

}]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59503386

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档