json类型转换对象含有泛型

发布时间:2024年01月13日

接受数据的对象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>

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

文章来源:https://blog.csdn.net/Takeit_easy/article/details/135568173
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。