fastjson转化复杂javabean
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了fastjson转化复杂javabean相关的知识,希望对你有一定的参考价值。
package json.fastjson;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
public class Fastjson2JavaBean {
public static void main(String[] args) {
Course math = new Course(1,"数学");
Course english = new Course(2,"英语");
List<Course> courseList = new ArrayList<Course>();
courseList.add(math);
courseList.add(english);
Student student = new Student("zz", courseList);
String json = JSONObject.toJSONString(student, SerializerFeature.WriteMapNullValue);
System.out.println(json);
Student s = JSON.parseObject(json, Student.class);
System.out.println(s.getName());
List<Course> list = s.getCourse();
for(int i = 0; i < list.size(); i++){
Course c = list.get(i);
System.out.println(c.getId() + "--" + c.getName());
}
}
}
class Student{
private String name;
private List<Course> course;
public Student(){
}
public Student(String name, List<Course> course){
this.name = name;
this.course = course;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Course> getCourse() {
return course;
}
public void setCourse(List<Course> course) {
this.course = course;
}
}
class Course{
private int id;
private String name;
public Course(){
}
public Course(int id, String name){
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
本文出自 “巧克力黑” 博客,请务必保留此出处http://10120275.blog.51cto.com/10110275/1866856
以上是关于fastjson转化复杂javabean的主要内容,如果未能解决你的问题,请参考以下文章