1 简单JSON与JavaBean的转换
1.1 设计Student实体类
package com.yiidian.domain;
/**
* 一点教程网 - http://www.yiidian.com
*/
public class Student {
private String studentName;
private Integer studentAge;
public Student() {
}
public Student(String studentName, Integer studentAge) {
this.studentName = studentName;
this.studentAge = studentAge;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Integer getStudentAge() {
return studentAge;
}
public void setStudentAge(Integer studentAge) {
this.studentAge = studentAge;
}
@Override
public String toString() {
return "Student{" +
"studentName='" + studentName + '\'' +
", studentAge=" + studentAge +
'}';
}
}
1.2 简单JSON转为JavaBean
MainApp:
package com.yiidian.fastjson;
import com.alibaba.fastjson.JSONObject;
import com.yiidian.domain.Student;
/**
* 一点教程网 - http://www.yiidian.com
*/
public class MainApp {
public static void main(String args[]){
String JSON_OBJ_STR = "{\"studentName\":\"lily\",\"studentAge\":12}";
Student student = JSONObject.parseObject(JSON_OBJ_STR, Student.class);
System.out.println(student);
}
}
运行效果为:
Student{studentName='lily', studentAge=12}
1.3