json抽象类转换 jsckson

1. 使用抽象类来接收json,实现一个抽象对象用多个实例对象获取

代码

导包

   <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.7</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.9.7</version>
    </dependency>

 

抽象类

package javastu;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        property = "type"
)
@JsonSubTypes({
        @JsonSubTypes.Type(name = "A", value = AStudent.class),
        @JsonSubTypes.Type(name = "B", value = BStudent.class)
})
public abstract class Student {

    private String age;

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}

实现类

package javastu;

public class AStudent extends Student{
    private String A;

    public String getA() {
        return A;
    }

    public void setA(String a) {
        A = a;
    }

    @Override
    public String toString() {
        return "AStudent{" +
                "A='" + A + '\'' +
                '}';
    }
}
package javastu;

public class BStudent extends Student{
    private String B;

    public String getB() {
        return B;
    }

    public void setB(String b) {
        B = b;
    }

    @Override
    public String toString() {
        return "BStudent{" +
                "B='" + B + '\'' +
                '}';
    }
}

测试

package javastu;


import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

public class App {

    public static void main(String[] args) throws IOException {
        AStudent studentA = new AStudent();
        studentA.setA("AA");
        studentA.setAge("111");

        BStudent studentB = new BStudent();
        studentB.setB("BB");
        studentB.setAge("222");

        ObjectMapper objectMapper = new ObjectMapper();

        String stuAStr = objectMapper.writeValueAsString((Student) studentA);
        String stuBStr = objectMapper.writeValueAsString(studentB);

        Student student2A = objectMapper.readValue(stuAStr, Student.class);
        Student student2B = objectMapper.readValue(stuBStr, Student.class);

        System.out.println(stuAStr);
        System.out.println(stuBStr);

        System.out.println(student2A instanceof AStudent);
        System.out.println(student2B instanceof BStudent);


    }
}

结果:

{"type":"A","age":"111","a":"AA"}
{"type":"B","age":"222","b":"BB"}
true
true

 

 

转载于:https://www.cnblogs.com/codeLei/p/11024208.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值