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;
}
}