JSON之FastJson

时间:2023-03-08 23:55:52
JSON之FastJson

FastJson是什么?

从网上查到---

官网地址:http://code.alibabatech.com/wiki/display/FastJSON/Overview(已关闭)

FastJSOn是阿里巴巴开源的JSON处理工具,包括“序列化”和“反序列化”两部分,它具备如下特征:

  1. 速度最快,测试表明,fastjson具有极快的性能,超越任其他的java json parser。包括自称最快的jackson。
  2. 功能强大,完全支持java bean、集合、Map、日期、Enum,支持范型,支持自省。
  3. 无依赖,能够直接运行在Java SE 5.0以上版本
  4. 支持Android。
  5. 开源 (Apache 2.0)

既然速度快,当然要体验一下。更何况是国产货,当然要支持了。测试一下,以后在项目里使用...

 package test;
import java.io.Serializable;
/**
* <一句话功能简述>
* @author aliger Email:liqimo#gmail.com
* @version [V1.00, 2014-8-16]
* @see [相关类/方法]
* @since V1.00
*/
public class UserInfo implements Serializable{
private String name;
private int age;
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age=age;
}
public int getAge(){
return age;
}
}

Main:

 package test;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;
/**
* <一句话功能简述>
*
* @author aliger Email:liqimo#gmail.com
* @version [V1.00, 2014-8-16]
* @see [相关类/方法]
* @since V1.00
*/
public class TestOne
{
public static FastJsonUtils ff; //public static void main(String[] args){
public static void test1(){
UserInfo info=new UserInfo();
info.setName("zhangsan");
info.setAge(24);
//将对象转换为JSON字符串
//String str_json=JSON.toJSONString(info);
String str_json=ff.getJsonString(info);
System.out.println("JSON="+str_json);
}
public static void test2() { HashMap<String, Object> map = new HashMap<String, Object>();
map.put("username", "zhangsan");
map.put("age", 24);
map.put("sex", "男");
//map集合
HashMap<String, Object> temp = new HashMap<String, Object>();
temp.put("name", "xiaohong");
temp.put("age", "23");
map.put("girlInfo", temp);
//list集合
List<String> list = new ArrayList<String>();
list.add("爬山");
list.add("骑车");
list.add("旅游");
map.put("hobby", list);
/*JSON 序列化,默认序列化出的JSON字符串中键值对是使用双引号,如果需要单引号的JSON字符串, [eg:String jsonString = JSON.toJSONString(map, SerializerFeature.UseSingleQuotes);]
*fastjson序列化时可以选择的SerializerFeature有十几个属性,你可以按照自己的需要去选择使用。
*/
String jsonString = JSON.toJSONString(map);
System.out.println("JSON===" + jsonString);
}
public static void test3(){
String json="{\"name\":\"chenggang\",\"age\":24}";
//反序列化
UserInfo userInfo=JSON.parseObject(json,UserInfo.class);
System.out.println("name:"+userInfo.getName()+", age:"+userInfo.getAge());
}
/**泛型的反序列化*/
public static void test4(){
String json="{\"user\":{\"name\":\"zhangsan\",\"age\":25}}";
Map<String, UserInfo> map = JSON.parseObject(json, new TypeReference<Map<String, UserInfo>>(){});
System.out.println(map.get("user"));
}
public static void test5(){
Date date=new Date();
//输出毫秒值
System.out.println(JSON.toJSONString(date));
//默认格式为yyyy-MM-dd HH:mm:ss
System.out.println(JSON.toJSONString(date, SerializerFeature.WriteDateUseDateFormat));
//根据自定义格式输出日期
System.out.println(JSON.toJSONStringWithDateFormat(date, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
}
public static void main(String[] args) {
test1();
test2();
test3();
test4();
test5();
}
}

out结果:

 JSON={"age":24,"name":"zhangsan"}
JSON==={"age":24,"girlInfo":{"age":"23","name":"xiaohong"},"hobby":["爬山","骑车","旅游"],"sex":"男","username":"zhangsan"}
name:chenggang, age:24
test.UserInfo@30c221
1408190003876
"2014-08-16 19:53:23"
"2014-08-16"