JSON-FastJson的基本使用方法
package com.hbbc.entity;
public class Student {
private int id;
private String stuName;
private int age;
// 使用fastjson要注意要转换的类必须有默认的无参构造方法。
public int getId() {
return id;
}
public Student() {
super();
}
public Student(int id, String stuName, int age) {
super();
this.id = id;
this.stuName = stuName;
this.age = age;
}
public void setId(int id) {
this.id = id;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.hbbc.entity;
public class Teacher {
private int id;
private String teaName;
private int age;
public Teacher() {
super();
}
public Teacher(int id, String teaName, int age) {
super();
this.id = id;
this.teaName = teaName;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTeaName() {
return teaName;
}
public void setTeaName(String teaName) {
this.teaName = teaName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
其他主要API:
public static final Object parse(Stringtext); // 把JSON文本parse为JSONObject或者JSONArray
public static final JSONObject parseObject(Stringtext); // 把JSON文本parse成JSONObject
public static final <T> T parseObject(Stringtext, Class<T> clazz); // 把JSON文本parse为JavaBean
public static final JSONArray parseArray(Stringtext); // 把JSON文本parse成JSONArray
public static final <T> List<T>parseArray(String text, Class<T> clazz); //把JSON文本parse成JavaBean集合
public static final String toJSONString(Objectobject); // 将JavaBean序列化为JSON文本
public static final String toJSONString(Objectobject, boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本
public static final Object toJSON(ObjectjavaObject); 将JavaBean转换为JSONObject或者JSONArray。