接受数据的对象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的版本文章来源:https://www.toymoban.com/news/detail-787333.html
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
从而实现指定泛型的类型就可以转换;提高复用性文章来源地址https://www.toymoban.com/news/detail-787333.html
到了这里,关于json类型转换对象含有泛型的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!