加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

fastjson常见用法

发布时间:2020-12-16 19:09:53 所属栏目:百科 来源:网络整理
导读:packagejms.test;importjava.util.ArrayList;importjava.util.Date;importjava.util.List;importjava.util.Map;importjms.model.Dept;importjms.model.Employee;importorg.junit.Test;importcom.alibaba.fastjson.JSON;importcom.alibaba.fastjson.JSONObjec
packagejms.test;
importjava.util.ArrayList;
importjava.util.Date;
importjava.util.List;
importjava.util.Map;
importjms.model.Dept;
importjms.model.Employee;
importorg.junit.Test;
importcom.alibaba.fastjson.JSON;
importcom.alibaba.fastjson.JSONObject;
importcom.alibaba.fastjson.TypeReference;
importcom.alibaba.fastjson.serializer.SerializeConfig;
importcom.alibaba.fastjson.serializer.SerializerFeature;
importcom.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
importcom.alibaba.fastjson.serializer.SimplePropertyPreFilter;
publicclassTestFastjson{
//fastjson序列化单个对象与反序列化
@Test
publicvoidtest1(){
Employeee=newEmployee("001","张三",23,newDate());

//序列化
StringjsonStr=JSON.toJSONString(e);
System.out.println(jsonStr);

//反序列化
Employeeemp=JSON.parSEObject(jsonStr,Employee.class);
System.out.println(emp.getName());
}

//fastjson序列化list集合与反序列化
@Test
publicvoidtest2(){
Employeee=newEmployee("001",newDate());
Employeee2=newEmployee("002","李四",29,newDate());

List<Employee>emps=newArrayList<Employee>();
emps.add(e);
emps.add(e2);

//fastjson序列化list,返回来的是一个json数组,由[]包含两个json
StringjsonArryStr=JSON.toJSONString(emps);
System.out.println(jsonArryStr);

////反序列化
//法一
//List<Employee>empList=JSON.parSEObject(jsonArryStr,newTypeReference<List<Employee>>(){});
//法二
List<Employee>empList=JSON.parseArray(jsonArryStr,Employee.class);
for(Employeeemployee:empList){
System.out.println(employee.getName());
System.out.println(employee.getBirthDay());
}


}

//fastjson序列化复杂对象与反序列化
@Test
publicvoidtest3(){
Employeee=newEmployee("001",newDate());

List<Employee>emps=newArrayList<Employee>();
emps.add(e);
emps.add(e2);

Deptdept=newDept("d001","研发部",emps);
//序列化
StringjsonStr=JSON.toJSONString(dept);
System.out.println(jsonStr);

//反序列化
Deptd=JSON.parSEObject(jsonStr,Dept.class);
System.out.println(d.getName());

//json转map
//法一
Map<String,Object>map1=JSON.parSEObject(jsonStr);//返回JSONObject,JSONObject实现Map<String,Object>接口
//法二
//Map<String,Object>map1=(Map<String,Object>)JSON.parse(jsonStr);
for(Stringkey:map1.keySet()){
System.out.println(key+":"+map1.get(key));
}
}

//fastjson的JSONObject的使用
@Test
publicvoidtest4(){
Employeee=newEmployee("001",newDate());

//序列化
StringjsonStr=JSON.toJSONString(e);
System.out.println(jsonStr);

//反序列化(可以和test1比较)
JSONObjectemp=JSON.parSEObject(jsonStr,JSONObject.class);
System.out.println(emp);
System.out.println(emp.getString("name"));

//再放一个Employee不存在的字段
emp.put("salary","8000");
System.out.println(emp.toJSONString());
System.out.println(emp.get("salary"));

}

//fastjson序列化字符串
@Test
publicvoidtest5(){

List<String>strs=newArrayList<String>();
strs.add("hello");
strs.add("world");
strs.add("banana");

//序列化
StringjsonStr=JSON.toJSONString(strs);
System.out.println(jsonStr);

//反序列化
List<String>strList=JSON.parSEObject(jsonStr,newTypeReference<List<String>>(){});
//List<String>strList=JSON.parseArray(jsonStr,String.class);//等同于上一句
for(Stringstr:strList){
System.out.println(str);
}
}

//fastjson过滤字段
@Test
publicvoidtest6(){

Employeee=newEmployee("001",newDate());

List<Employee>emps=newArrayList<Employee>();
emps.add(e);
emps.add(e2);

//构造过滤器
SimplePropertyPreFilterfilter=newSimplePropertyPreFilter(Employee.class,"id","age");
StringjsonStr=JSON.toJSONString(emps,filter);

System.out.println(jsonStr);
}


//fastjson日期处理
@Test
publicvoidtest7(){

Datedate=newDate();

StringdateStr=JSON.toJSONString(date);
System.out.println(dateStr);

StringdateStr2=JSON.toJSONStringWithDateFormat(date,"yyyy-MM-ddHH:mm:ss");
System.out.println(dateStr2);

//序列化实体
Employeeemp=newEmployee("001",newDate());

//法一
StringempStr=JSON.toJSONStringWithDateFormat(emp,"yyyy-MM-ddHH:mm:ss");
System.out.println(empStr);
//法二
StringempStr2=JSON.toJSONString(emp,SerializerFeature.WriteDateUseDateFormat);
System.out.println(empStr2);

//法三
SerializeConfigconfig=newSerializeConfig();
config.put(Date.class,newSimpleDateFormatSerializer("yyyy年MM月dd日HH时mm分ss秒"));
StringempStr3=JSON.toJSONString(emp,config);
System.out.println(empStr3);
}

//fastjson去掉值的双引号实现JSONAware接口
@Test
publicvoidtest8(){
//见同级目录的Function.java
}

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读