FastJson基本用法
来源:互联网 发布:晨枫网络是干嘛的 编辑:程序博客网 时间:2024/06/07 01:21
基本用法:String objJson = JSON.toJSONString(Object object);
例一:解析基本json格式
例二:解析list
例三:解析复杂格式
例四:自定义需要序列化字段
例五:反序列化
例六:反序列化,复杂转换
例七:解析Map
例八:解析List嵌套Map
例九:对日期进行格式化
例十:输出为null的字段(FastJSON不输入为值Null的字段)
public class Fastjson { public static void main(String[] args) { //Student类和teacher类放在最后,需要get、set、需要空参构造和有参构造,toString方法。 //例一:解析基本json格式 //创建一个student对象 Student student = new Student(0,18,"sun"); System.out.println(student); System.out.println(JSON.toJSONString(student)); //true会格式化输出样式 System.out.println(JSON.toJSONString(student,true)); //SerializerFeature属性:PrettyFormat格式化;(效果等同于true) System.out.println(JSON.toJSONString(student,SerializerFeature.PrettyFormat)); //例二:解析list //创建一个students集合 List<Student> students = new ArrayList<Student>(); for (int i = 0; i < 3; i++) { Student st = new Student(i,18+i,"sun"+i); students.add(st); } System.out.println(JSON.toJSONString(students)); //例三:解析复杂格式 //创建一个teacher集合(包含students1集合) List<Teacher> teachers = new ArrayList<Teacher>(); for (int i = 0; i < 2; i++) { Teacher teacher = new Teacher(i,"teacher"+i); List<Student> students1 = new ArrayList<Student>(); for (int j = 0; j < 3; j++) { Student st = new Student(j,18+j,"sun"+j); students1.add(st); } teacher.setStudnets(students1); teachers.add(teacher); } System.out.println(JSON.toJSONString(teachers)); //序列化就是指 把JavaBean对象转成JSON格式的字符串。 //例四:自定义需要序列化字段 SimplePropertyPreFilter simple = new SimplePropertyPreFilter(Student.class,"id","age"); System.out.println(JSON.toJSONString(students,simple)); //例五:反序列化 //需要对应的class有默认构造方法,不然转换会报错(default constructor not found. class csdn.fastjson.Student) String string = JSON.toJSONString(student); System.out.println(JSON.parseObject(string,Student.class)); //例六:反序列化,复杂转换 //fastjson框架的 TypeReference 可将json串转成定义好的泛型对象 String str = JSON.toJSONString(students); List<Student> stu = JSON.parseObject(str, new TypeReference<List<Student>>(){}); for(int i=0;i<stu.size();i++) { System.out.println(stu.get(i)); } //例七:解析Map //创建一个map集合 Map<String,Object> map = new HashMap<String,Object>(); map.put("id", 1); map.put("姓名", "太阳"); map.put("年龄", 18); System.out.println(JSON.toJSON(map)); //例八:解析List<Map<String,Object>> //list嵌套map List<Map<String,Object>> list = new ArrayList<Map<String,Object>>(); for (int i = 0; i < 3; i++) { Map<String,Object> m = new HashMap<String,Object>(); m.put("id", i); m.put("姓名", "姓名"+i); m.put("年龄", 18+i); list.add(m); } System.out.println(JSON.toJSON(list)); //例九:对日期进行格式化 //在没有设置格式的时候,FastJSON会将Date转成long System.out.println(JSON.toJSONString(new Date())); //SerializerFeature格式化日期 System.out.println(JSON.toJSONString(new Date(), SerializerFeature.WriteDateUseDateFormat)); //指定输出日期的格式 System.out.println(JSON.toJSONStringWithDateFormat(new Date(), "yyyy-MM-dd HH:mm:ss.SSS")); //例十:输出为null的字段(FastJSON不输入为值Null的字段) map.put("学历", null); map.put("籍贯", null); students.add(new Student(10,10,null)); System.out.println("例九:输出为null的字段(FastJSON不输入为值Null的字段)"); System.out.println(JSON.toJSONString(map)); System.out.println(JSON.toJSONString(map, SerializerFeature.WriteMapNullValue)); System.out.println(JSON.toJSONString(students)); System.out.println(JSON.toJSONString(students, SerializerFeature.WriteMapNullValue)); }}
运行结果:
student [id=0, age=18, name=sun]
例一:解析基本json格式
{“age”:18,”id”:0,”name”:”sun”}
例一:true会格式化输出样式
{
“age”:18,
“id”:0,
“name”:”sun”
}
例一:SerializerFeature.PrettyFormat
{
“age”:18,
“id”:0,
“name”:”sun”
}
例二:解析list
[{“age”:18,”id”:0,”name”:”sun0”},{“age”:19,”id”:1,”name”:”sun1”},{“age”:20,”id”:2,”name”:”sun2”}]
例三:解析复杂格式
[{“id”:0,”name”:”teacher0”,”studnets”:[{“age”:18,”id”:0,”name”:”sun0”},{“age”:19,”id”:1,”name”:”sun1”},{“age”:20,”id”:2,”name”:”sun2”}]},{“id”:1,”name”:”teacher1”,”studnets”:[{“age”:18,”id”:0,”name”:”sun0”},{“age”:19,”id”:1,”name”:”sun1”},{“age”:20,”id”:2,”name”:”sun2”}]}]
例四:自定义需要解析字段,自定义id,age
[{“age”:18,”id”:0},{“age”:19,”id”:1},{“age”:20,”id”:2}]
例五:反序列化
student [id=0, age=18, name=sun]
例六:反序列化,复杂转换
student [id=0, age=18, name=sun0]
student [id=1, age=19, name=sun1]
student [id=2, age=20, name=sun2]
例七:解析Map
{“id”:1,”年龄”:18,”姓名”:”太阳”}
例八:解析List嵌套Map
[{“id”:0,”年龄”:18,”姓名”:”姓名0”},{“id”:1,”年龄”:19,”姓名”:”姓名1”},{“id”:2,”年龄”:20,”姓名”:”姓名2”}]
例九:对日期进行格式化(无格式)
1508315881771
例九:SerializerFeature格式化日期
“2017-10-18 16:38:01”
例九:指定输出日期的格式
“2017-10-18 16:38:01.812”
例十:输出为null的字段(FastJSON不输入为值Null的字段)
{“id”:1,”姓名”:”太阳”,”年龄”:18}
{“id”:1,”姓名”:”太阳”,”籍贯”:null,”学历”:null,”年龄”:18}
[{“age”:18,”id”:0,”name”:”sun0”},{“age”:19,”id”:1,”name”:”sun1”},{“age”:20,”id”:2,”name”:”sun2”},{“age”:10,”id”:10}]
[{“age”:18,”id”:0,”name”:”sun0”},{“age”:19,”id”:1,”name”:”sun1”},{“age”:20,”id”:2,”name”:”sun2”},{“age”:10,”id”:10,”name”:null}]
- FastJson基本用法
- fastjson 基本使用方法
- FastJson的基本使用
- FastJson的基本使用
- FastJson的基本使用
- FastJson的基本使用
- fastjson的基本使用方法
- fastJson的基本使用
- FastJson的基本使用
- FastJson的基本使用
- FastJson基本使用
- Fastjson的基本使用
- FastJson的基本使用
- FastJson的基本使用
- fastjson的基本使用方法
- FastJson的基本使用
- FastJson基本使用
- fastjson的基本使用方法
- 常用 vim/终端 命令-----笔记
- 数据结构与算法Java版——中缀表达式转后缀表达式
- Android开发知识(九):Android事件处理机制:事件分发、传递、拦截、处理机制的原理分析(下)
- WIN编程书籍总结
- opencv 16 OCR 端到端场景文本检测与识别(webcam_demo) vs2015
- FastJson基本用法
- Java SE(四)——对象、类、接口 (第二个版本)
- ConfigReader(四十八)—— ReadSkillArea
- MVC程序设计实例
- Spring定时器spring task踩过的坑
- 【Android】Retrofit 2.0 的使用
- IPC机制系列之三 Android中的IPC方式 (ContentProvider,Socket)
- H
- openstack判断是否支持numa