fastjson API 入口類是com.alibaba.fastjson.JSON,常用的序列化操作都可以在JSON類上的靜態(tài)方法直接完成。
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轉(zhuǎn)換為JSONObject或者JSONArray。
使用方法舉例
//將JSON文本轉(zhuǎn)換為java對象
import com.alibaba.fastjson.JSON;
Model model = JSON.parseObject(jsonStr, Model.class);
有關類庫的一些說明
JSONArray : 相當于List
JSONObject: 相當于Map
fastjson 使用實例java對象與JSON字符串的互轉(zhuǎn)
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對象轉(zhuǎn) json字符串
*/
@Test
public void objectTOJson(){
//簡單java類轉(zhuǎn)json字符串
User user = new User("dmego", "123456");
String UserJson = JSON.toJSONString(user);
System.out.println("簡單java類轉(zhuǎn)json字符串:"+UserJson);
//List
輸出結果
簡單java類轉(zhuǎn)json字符串:{"password":"123456","username":"dmego"}
List轉(zhuǎn)json字符串:[{"password":"123123","username":"zhangsan"},{"password":"321321","username":"lisi"}]
復雜java類轉(zhuǎn)json字符串:{"name":"userGroup","users":[{"password":"123123","username":"zhangsan"},{"password":"321321","username":"lisi"}]}
json字符串轉(zhuǎn)簡單java對象:User [username=dmego, password=123456]
json字符串轉(zhuǎn)List對象:[User [username=zhangsan, password=123123], User [username=lisi, password=321321]]
json字符串轉(zhuǎn)復雜java對象:UserGroup [name=userGroup, users=[User [username=zhangsan, password=123123], User [username=lisi, password=321321]]]