首页 > Web开发 > 详细

json抽象类转换 jsckson

时间:2019-06-14 17:25:07      阅读:439      评论:0      收藏:0      [点我收藏+]

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

 

 

json抽象类转换 jsckson

原文:https://www.cnblogs.com/codeLei/p/11024208.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!