up:: Java处理JSON(FastJson简介)


主要内容是Java中的利用FastJson对JSON的序列化与反序列化。

一:将对象转换成JSON字符串(序列化)

示例一:将对象转换成JSON字符串

javaBean:

    import java.util.Date;
 
    public class Employee {
    	private Integer empno;
    	private String ename;
    	private String job;
    	private Date hdate;
    	private Float salary;
    	private String dname;
 
    	public Employee() {}
 
    	public Employee(Integer empno, String ename, String job, Date hdate, Float salary, String dname) {
    		super();
    		this.empno = empno;
    		this.ename = ename;
    		this.job = job;
    		this.hdate = hdate;
    		this.salary = salary;
    		this.dname = dname;
    	}
 
    	public Integer getEmpno() {
    		return empno;
    	}
    	public void setEmpno(Integer empno) {
    		this.empno = empno;
    	}
    	public String getEname() {
    		return ename;
    	}
    	public void setEname(String ename) {
    		this.ename = ename;
    	}
    	public String getJob() {
    		return job;
    	}
    	public void setJob(String job) {
    		this.job = job;
    	}
    	public Date getHdate() {
    		return hdate;
    	}
    	public void setHdate(Date hdate) {
    		this.hdate = hdate;
    	}
    	public Float getSalary() {
    		return salary;
    	}
    	public void setSalary(Float salary) {
    		this.salary = salary;
    	}
    	public String getDname() {
    		return dname;
    	}
    	public void setDname(String dname) {
    		this.dname = dname;
    	}
    }

示例程序:

 
 
    import java.util.Calendar;
 
    import com.alibaba.fastjson.JSON;
 
    public class FastJsonSample1 {
 
    	public static void main(String[] args) {
 
    		Employee employee = new Employee();
    		employee.setEmpno(5566);
    		employee.setEname("刘芳");
    		employee.setJob("运维工程师");
    		employee.setSalary(10000f);
    		employee.setDname("工程部");
    		Calendar c = Calendar.getInstance();
    		// 月份是从0开始的,所以5代表六月;
    		c.set(2020, 5, 16, 10, 38, 0);
    		employee.setHdate(c.getTime());
 
    		// FastJson中提供了JSON对象,完成对象与JSON字符串的互相转换
    		String jsonString = JSON.toJSONString(employee);
    		System.out.println(jsonString);
    	}
 
    }
 

结果:

 
    {"dname":"工程部","empno":5566,"ename":"刘芳","hdate":1592275080504,"job":"运维工程师","salary":10000.0}

一:附: @JSONField注解

发现,上面的hdate值是:1970年至当前设置时间的毫秒数,很难看懂;为了解决这个问题,需要用JSON注解来对日期格式进行格式化输出: @JSONField注解

此时再次运行的效果:

 
    {"dname":"工程部","empno":5566,"ename":"刘芳","hdate":"2020-06-16 10:38:00 229","job":"运维工程师","salary":10000.0}

此时效果:

    {"dname":"工程部","empno":5566,"ename":"刘芳","hdate":"2020-06-16","job":"运维工程师","salary":10000.0}

@JSONField注解还可以对输出的key进行描述:

如下在现实时,将不太容易明白什么意思的hdate换成了hiredate;

@JSONField注解还可以取消个别属性的JSON序列化:

如下,dname这个属性不重要,在转成JSON时不想要dname属性了:


二:将JSON字符串转换为对象(反序列化)

Employee emp = JSON.parseObject(jsonString, Employee.class):第一个参数为待转换的JSON字符串,第二个参数为想要转换的类;

    package com.imooc.json;
 
    import java.util.Calendar;
 
    import com.alibaba.fastjson.JSON;
 
    public class FastJsonSample1 {
 
    	public static void main(String[] args) {
 
    		Employee employee = new Employee();
    		employee.setEmpno(5566);
    		employee.setEname("刘芳");
    		employee.setJob("运维工程师");
    		employee.setSalary(10000f);
    		employee.setDname("工程部");
    		Calendar c = Calendar.getInstance();
    		// 月份是从0开始的,所以5代表六月;
    		c.set(2020, 5, 16, 10, 38, 0);
    		employee.setHdate(c.getTime());
 
    		// FastJson中提供了JSON对象,完成对象与JSON字符串的互相转换
    		String jsonString = JSON.toJSONString(employee);
    		System.out.println(jsonString);
 
    		// 将字符串反序列化为对象
    		Employee emp = JSON.parseObject(jsonString, Employee.class);
    		System.out.println(emp.getEname());
 
    	}
    }