json类型转换对象含有泛型

接受数据的对象1

public class Student<T> {
    /**
     * 学号
     */
    private String studentNumber;

    public String getStudentNumber() {
        return studentNumber;
    }

    public void setStudentNumber(String studentNumber) {
        this.studentNumber = studentNumber;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public List<T> getBooks() {
        return books;
    }

    public void setBooks(List<T> books) {
        this.books = books;
    }

    @Override
    public String toString() {
        return "Student{" +
                "studentNumber='" + studentNumber + '\'' +
                ", studentName='" + studentName + '\'' +
                ", books=" + books +
                '}';
    }

    public Student(String studentNumber, String studentName, List<T> books) {
        this.studentNumber = studentNumber;
        this.studentName = studentName;
        this.books = books;
    }

    /**
     * 学生姓名
     */
    private String studentName;
    private List<T> books;
}

对象1里有list 泛型
因为此时接受的是Book类的数据,所以我转换的时候指定了转换类型为Book类型
接受的其他类型的数据,可以转换的时候指定这个类型就可以了

public class Book {
    private String bookName;
    private String price;

    @Override
    public String toString() {
        return "Book{" +
                "bookName='" + bookName + '\'' +
                ", price='" + price + '\'' +
                ", type='" + type + '\'' +
                '}';
    }

    private String type;

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Book(String bookName, String price, String type) {
        this.bookName = bookName;
        this.price = price;
        this.type = type;
    }
}

实操代码

  String parse="{\n" +
                "  \"books\": [\n" +
                "    {\n" +
                "      \"bookName\": \"Book 1\",\n" +
                "      \"price\": \"19.99\",\n" +
                "      \"type\": \"Fiction\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"bookName\": \"Book 2\",\n" +
                "      \"price\": \"29.99\",\n" +
                "      \"type\": \"Non-fiction\"\n" +
                "    }\n" +
                "  ],\n" +
                "  \"studentName\": \"John Doe\",\n" +
                "  \"studentNumber\": \"123456\"\n" +
                "}";
        JSONObject parseObject=JSONObject.parseObject(parse);

        Student student = JSONObject.parseObject(parse,Student.class);
        System.out.println(student);
        List<Book> books = JSONObject.parseArray(parseObject.get("books").toString(), Book.class);
        student.setBooks(books);
        System.out.println(student);
        Student<Book> student1 = JSONObject.parseObject(parse, new TypeReference<Student<Book>>() {});
        System.out.println(student1);

输出结果

Student{studentNumber='123456', studentName='John Doe', books=[{"price":"19.99","type":"Fiction","bookName":"Book 1"}, {"price":"29.99","type":"Non-fiction","bookName":"Book 2"}]}
Student{studentNumber='123456', studentName='John Doe', books=[Book{bookName='Book 1', price='19.99', type='Fiction'}, Book{bookName='Book 2', price='29.99', type='Non-fiction'}]}
Student{studentNumber='123456', studentName='John Doe', books=[Book{bookName='Book 1', price='19.99', type='Fiction'}, Book{bookName='Book 2', price='29.99', type='Non-fiction'}]}

TypeReference是在运行时指定要转换的类型包括泛型

fastjson的版本

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.76</version>
</dependency>

从而实现指定泛型的类型就可以转换;提高复用性

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值