简介
阿里巴巴FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具备如下特征:
速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java Json parser。包括自称最快的JackJson;
功能强大,完全支持Java Bean、集合、Map、日期、Enum,支持范型,支持自省;无依赖,能够直接运行在Java SE 5.0以上版本;支持Android;开源 (Apache 2.0)
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。
搭建
一、首先,添加Maven依赖。在pom.xml文件中添加以下代码。
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>
二、创建Test测试类。
/**
*
* @Title: test_01
* @Description: 把json数据反序列化为对象
* @return void
*/
@Test
public void test_01(){
String json = "{"birthday":"199507133010","name":"allen","password":"21312312","userExtend":{"clicklike":"123"}}";
User user = (User)JSON.parseObject(json, User.class);
System.out.println(user);
// com.allen.entity.User@7a5d012c
}
/**
*
* @Title: test_02
* @Description: 把对象 序列化为 json数据
* @return void
*/
@Test
public void test_02(){
User user = new User("allen","21312312","199507133010",new UserExtend("123"));
System.out.println(JSON.toJSONString(user));
// {"birthday":"199507133010","name":"allen","password":"21312312","userExtend":{"clicklike":"123"}}
}
/**
*
* @Title: test_03
* @Description: 把数组或者list 序列化为 json数据
* @return void
*/
@Test
public void test_03(){
List<User> list = new ArrayList<User>();
User u = new User("allen","21312312","199507133010",new UserExtend("123"));
User v = new User("bebe","21312312","199507133010",new UserExtend("123"));
User w = new User("Amy","21312312","199507133010",new UserExtend("123"));
list.add(u);
list.add(v);
list.add(w);
String json = JSON.toJSONString(list);
System.out.println(json);
// [
// {"birthday":"199507133010","name":"allen","password":"21312312","userExtend":{"clicklike":"123"}}
// ,{"birthday":"199507133010","name":"bebe","password":"21312312","userExtend":{"clicklike":"123"}}
// ,{"birthday":"199507133010","name":"Amy","password":"21312312","userExtend":{"clicklike":"123"}}
// ]
}
/**
*
* @Title: test_04
* @Description: 把对象 序列化为 json数据
* @return void
*/
@Test
public void test_04(){
String json = "[{\"birthday\":\"199507133010\",\"name\":\"allen\",\"password\":\"21312312\",\"userExtend\":{\"clicklike\":\"123\"}},{\"birthday\":\"199507133010\",\"name\":\"bebe\",\"password\":\"21312312\",\"userExtend\":{\"clicklike\":\"123\"}},{\"birthday\":\"199507133010\",\"name\":\"Amy\",\"password\":\"21312312\",\"userExtend\":{\"clicklike\":\"123\"}}]";
List<User> list = JSON.parseArray(json, User.class);
System.out.println(list);
// [com.allen.entity.User@7a5d012c, com.allen.entity.User@3fb6a447, com.allen.entity.User@79b4d0f]
}