加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

fastjson转化复杂javabean

发布时间:2020-12-16 18:53:21 所属栏目:百科 来源:网络整理
导读: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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读