1. 预先导入
/*json库 实体 <-> json*/
compile('com.alibaba:fastjson:1.2.51')
2. 实体类准备
package com.zr.domain;
import lombok.Getter;
import lombok.Setter;
import java.sql.Date;
import java.text.SimpleDateFormat;
/**
* 学生实体定义
* @author zhurui
*/
public class Student{
@Getter @Setter
private long id;
@Getter @Setter
private long studentNumber;
@Getter @Setter
private String name;
@Getter @Setter
private String gender;
@Getter @Setter
private String studentClass;
@Getter @Setter
private java.sql.Date birthday;
public Student(long id, long studentNumber, String name, String gender, String studentClass, Date birthday) {
this.id = id;
this.studentNumber = studentNumber;
this.name = name;
this.gender = gender;
this.studentClass = studentClass;
this.birthday = birthday;
}
@Override
public String toString() {
return "{" +
"'id':'" + id + "'" +
", 'studentNumber':'" + studentNumber + "'" +
", 'name':'" + name + "'" +
", 'gender':'" + gender + "'" +
", 'studentClass':'" + studentClass + "'" +
", 'birthday':'" + new SimpleDateFormat("yyyy-MM-dd").format(birthday) + "'" +
'}';
}
}
注意
标准格式的json字符串,即{"k":"v","k":"v","k":"v"...............................}
3.调用
public HashMap getInfo() {
return JSON.parseObject(student.toString(), HashMap.class);
}