fastjson转化复杂javabean

前端之家收集整理的这篇文章主要介绍了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; }}

原文链接:https://www.f2er.com/json/289138.html

猜你在找的Json相关文章