fastjson API 入口类是com.alibaba.fastjson.JSON,常用的序列化操作都可以在JSON类上的静态方法直接完成。
public static final Object parse(String text); // 把JSON文本parse为JSONObject或者JSONArray
public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject
public static final T parseObject(String text, Class clazz); // 把JSON文本parse为JavaBean
public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray
public static final List parseArray(String text, Class clazz); //把JSON文本parse成JavaBean集合
public static final String toJSONString(Object object); // 将JavaBean序列化为JSON文本
public static final String toJSONString(Object object, boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本
public static final Object toJSON(Object javaObject); //将JavaBean转换为JSONObject或者JSONArray。
使用方法举例
//将JSON文本转换为java对象
import com.alibaba.fastjson.JSON;
Model model = JSON.parseObject(jsonStr, Model.class);
有关类库的一些说明
JSONArray : 相当于List
JSONObject: 相当于Map
fastjson 使用实例java对象与JSON字符串的互转
User测试类
/**
* User测试类
* @author dmego
*/
public class User {
private String username;
private String password;
public User(){}
public User(String username,String password){
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + "]";
}
}
UserGroup测试类
import java.util.ArrayList;
import java.util.List;
/**
* 用户组测试类
* @author dmego
*
*/
public class UserGroup {
private String name;
private List users = new ArrayList();
public UserGroup(){}
public UserGroup(String name,List users){
this.name = name;
this.users = users;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getUsers() {
return users;
}
public void setUsers(List users) {
this.users = users;
}
@Override
public String toString() {
return "UserGroup [name=" + name + ", users=" + users + "]";
}
}
fastJson测试类
package demo;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.alibaba.fastjson.JSON;
/**
* fastJson测试类
* @author dmego
*
*/
public class TestFastJosn {
/**
* java对象转 json字符串
*/
@Test
public void objectTOJson(){
//简单java类转json字符串
User user = new User("dmego", "123456");
String UserJson = JSON.toJSONString(user);
System.out.println("简单java类转json字符串:"+UserJson);
//List
输出结果
简单java类转json字符串:{"password":"123456","username":"dmego"}
List转json字符串:[{"password":"123123","username":"zhangsan"},{"password":"321321","username":"lisi"}]
复杂java类转json字符串:{"name":"userGroup","users":[{"password":"123123","username":"zhangsan"},{"password":"321321","username":"lisi"}]}
json字符串转简单java对象:User [username=dmego, password=123456]
json字符串转List对象:[User [username=zhangsan, password=123123], User [username=lisi, password=321321]]
json字符串转复杂java对象:UserGroup [name=userGroup, users=[User [username=zhangsan, password=123123], User [username=lisi, password=321321]]]