分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

FastJson简单使用

发布时间:2023-09-06 01:43责任编辑:白小东关键词:暂无标签

首先建立两个实体类,Student.java 和 Teacher.java

public class Student { ???????????private int id; ?????private String name; ?????private int age; ???????????/** ?????* 默认的构造方法必须不能省,不然不能解析 ?????*/ ???????????public Student(){ ???????????????} ?????public Student(int id,String name,int age) { ?????????this.id = id; ?????????this.name = name; ?????????this.age = age; ?????} ???????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; ?????} ???????public int getAge() { ?????????return age; ?????} ???????public void setAge(int age) { ?????????this.age = age; ?????} ???????????@Override ?????public String toString() { ?????????return "Student [id=" + id + ", name=" + name + ", age=" + age + "]"; ?????} ???????} ?
public class Teacher { ???????????private int id; ?????private String name; ???????????private List<Student> students; ?????????????/** ?????* 默认的构造方法必须不能省,不然不能解析 ?????*/ ?????public Teacher() { ???????????????} ?????public Teacher(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; ?????} ???????public List<Student> getStudents() { ?????????return students; ?????} ?????public void setStudents(List<Student> students) { ?????????this.students = students; ?????} ?????@Override ?????public String toString() { ?????????return "Teacher [id=" + id + ", name=" + name + ", mStudents=" ?????????????????+ students + "]"; ?????} ?????????????} ?

对象转为json串

public class App { ???public static void main(String[] args) { ???????Student student = new Student(0, "Aaron", 24); ???????System.out.println(JSON.toJSONString(student)); ???????List<Student> students = new ArrayList<Student>(); ???????for(int i=0;i<5;i++) { ???????????Student stu = new Student(i, "Student" + i, 18 +i); ???????????students.add(stu); ???????} ???????System.out.println(JSON.toJSONString(students)); ???????List<Teacher> teaList = new ArrayList<Teacher>(); ???????long time = System.currentTimeMillis(); ???????for(int i=0;i<10;i++) { ???????????Teacher teacher = new Teacher(i, "Teacher " + i); ???????????List<Student> stus = new ArrayList<Student>(); ???????????for(int j = 0 ;j<4;j++) { ???????????????Student s = new Student(j, "Student" + j, 18 +j); ???????????????stus.add(s); ???????????} ???????????teacher.setStudents(stus); ???????????teaList.add(teacher); ???????} ???????String jsonTeach = JSON.toJSONString(teaList); ???????System.out.println("fastjson = " + jsonTeach); ???????System.out.println("=========================================="); ???????Student student1 = new Student(0, "Aaron", 24); ???????System.out.println(JSON.toJSONString(student1,true)); ???}}

json串转为对象

public class TestParseToObject { ???public static void main(String[] args) { ???????Student student = new Student(0, "Aaron", 24); ???????String str = JSON.toJSONString(student,true); ???????System.out.println(JSON.parseObject(str,Student.class)); ???????System.out.println("================================================="); ???????List<Student> students = new ArrayList<Student>(); ???????for(int i=0;i<5;i++) { ???????????Student stu = new Student(i, "Student" + i, 18 +i); ???????????students.add(stu); ???????} ???????// 过滤哪些属性需要转换// ?????SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Student.class, "id","age");// ?????String jsonStu =JSON.toJSONString(students,filter); ???????String jsonStu = JSON.toJSONString(students); ???????System.out.println(jsonStu);// ???????List<Student> stu =JSON.parseObject(jsonStu, new TypeReference<List<Student>>(){}); ???????List<Student> stu =JSON.parseArray(jsonStu, Student.class); ???????for(int i=0;i<stu.size();i++) ???????{ ???????????System.out.println(stu.get(i)); ???????} ???}}

日期相关

1.日期格式化:

FastJSON可以直接对日期类型格式化,在缺省的情况下,FastJSON会将Date转成long。

例5:FastJSON将java.util.Date转成long。

1 String dateJson = JSON.toJSONString(new Date());2 ????????3 System.out.println(dateJson);

输出结果:

1401370199040

例6:使用SerializerFeature特性格式化日期。

1 String dateJson = JSON.toJSONString(new Date(), SerializerFeature.WriteDateUseDateFormat);2 ????????3 System.out.println(dateJson);

输出结果:

"2014-05-29 21:36:24"

也可以指定输出日期格式。

例7:指定输出日期格式。

1 String dateJson = JSON.toJSONStringWithDateFormat(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");2 ????????3 System.out.println(dateJson);

输出结果:

"2014-05-29 21:47:00.154"

2.使用单引号。

例8:以例2为例。

String listJson = JSON.toJSONString(list, SerializerFeature.UseSingleQuotes);

输出结果:

[{‘key1‘:‘One‘,‘key2‘:‘Two‘},{‘key3‘:‘Three‘,‘key4‘:‘Four‘}]

3.JSON格式化。

例9:

String listJson = JSON.toJSONString(list, SerializerFeature.PrettyFormat);

输出结果:与例4结果一致。

4.输出Null字段。

 缺省情况下FastJSON不输入为值Null的字段,可以使用SerializerFeature.WriteMapNullValue使其输出。

例10:

1 Map<String, Object> map = new HashMap<String,Object>();2 ????????3 String b = null;4 Integer i = 1;5 ????????6 map.put("a", b);7 map.put("b", i);8 ????????9 String listJson = JSON.toJSONString(map, SerializerFeature.WriteMapNullValue);

输出结果:

{"a":null,"b":1}

JSONObject,JSONArray是JSON的两个子类

JSONObject相当于Map<String, Object>,

JSONArray相当于List<Object>。

简单方法示例:

例16:将Map转成JSONObject,然后添加元素,输出。

 1 Map<String, Object> map = new HashMap<String, Object>(); 2 map.put("key1", "One"); 3 map.put("key2", "Two"); 4 ????????????????????????????5 JSONObject j = new JSONObject(map); 6 ????????????????????7 j.put("key3", "Three"); 8 ???????????9 System.out.println(j.get("key1"));10 System.out.println(j.get("key2"));11 System.out.println(j.get("key3"));

输出结果:

1 One2 Two3 Three

例17:将List对象转成JSONArray,然后输出。

 1 List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); 2 ?????????3 Map<String, Object> map = new HashMap<String, Object>(); 4 map.put("key1", "One"); 5 map.put("key2", "Two"); 6 ?????????7 Map<String, Object> map2 = new HashMap<String, Object>(); 8 map2.put("key1", "Three"); 9 map2.put("key2", "Four");10 ????????11 list.add(map);12 list.add(map2);13 ????????14 JSONArray j = JSONArray.parseArray(JSON.toJSONString(list));15 ?????????16 for(int i=0; i<j.size(); i++){17 ????System.out.println(j.get(i));18 }

输出结果:

1 {"key1":"One","key2":"Two"}2 {"key1":"Three","key2":"Four"}

FastJson简单使用

原文地址:https://www.cnblogs.com/zailushang1996/p/8480675.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved