码迷,mamicode.com
首页 > Web开发 > 详细

FastJson简单使用

时间:2018-02-27 23:40:28      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:seda   for   one   日期类型   建立   stat   rgs   过滤   test   

首先建立两个实体类,Student.java 和 Teacher.java

public class Student {  
      
    private int id;  
    private String name;  
    private int age;  
      
    /** 
     * 默认的构造方法必须不能省,不然不能解析 
     */  
      
    public Student(){  
          
    }  
    public Student(int id,String name,int age) {  
        this.id = id;  
        this.name = name;  
        this.age = age;  
    }  
  
    public int getId() {  
        return id;  
    }  
  
    public void setId(int id) {  
        this.id = id;  
    }  
  
    public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  
  
    public int getAge() {  
        return age;  
    }  
  
    public void setAge(int age) {  
        this.age = age;  
    }  
      
    @Override  
    public String toString() {  
        return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";  
    }  
      
}  
public class Teacher {  
      
    private int id;  
    private String name;  
      
    private List<Student> students;  
      
  
    /** 
     * 默认的构造方法必须不能省,不然不能解析 
     */  
    public Teacher() {  
          
    }  
    public Teacher(int id,String name) {  
        this.id = id;  
        this.name = name;  
    }  
      
    public int getId() {  
        return id;  
    }  
    public void setId(int id) {  
        this.id = id;  
    }  
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
  
    public List<Student> getStudents() {  
        return students;  
    }  
    public void setStudents(List<Student> students) {  
        this.students = students;  
    }  
    @Override  
    public String toString() {  
        return "Teacher [id=" + id + ", name=" + name + ", mStudents="  
                + students + "]";  
    }  
      
      
}  

对象转为json串

public class App 
{
    public static void main(String[] args) {
        Student student = new Student(0, "Aaron", 24);
        System.out.println(JSON.toJSONString(student));

        List<Student> students = new ArrayList<Student>();
        for(int i=0;i<5;i++) {
            Student stu = new Student(i, "Student" + i, 18 +i);
            students.add(stu);
        }
        System.out.println(JSON.toJSONString(students));

        List<Teacher> teaList = new ArrayList<Teacher>();
        long time = System.currentTimeMillis();
        for(int i=0;i<10;i++) {
            Teacher teacher = new Teacher(i, "Teacher " + i);
            List<Student> stus = new ArrayList<Student>();
            for(int j = 0 ;j<4;j++) {
                Student s = new Student(j, "Student" + j, 18 +j);
                stus.add(s);
            }
            teacher.setStudents(stus);
            teaList.add(teacher);
        }
        String jsonTeach = JSON.toJSONString(teaList);
        System.out.println("fastjson = " + jsonTeach);
        System.out.println("==========================================");
        Student student1 = new Student(0, "Aaron", 24);
        System.out.println(JSON.toJSONString(student1,true));

    }
}

 

json串转为对象

public class TestParseToObject {
    public static void main(String[] args) {

        Student student = new Student(0, "Aaron", 24);
        String str = JSON.toJSONString(student,true);
        System.out.println(JSON.parseObject(str,Student.class));
        System.out.println("=================================================");

        List<Student> students = new ArrayList<Student>();
        for(int i=0;i<5;i++) {
            Student stu = new Student(i, "Student" + i, 18 +i);
            students.add(stu);
        }
        // 过滤哪些属性需要转换
//      SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Student.class, "id","age");
//      String jsonStu =JSON.toJSONString(students,filter);
        String jsonStu = JSON.toJSONString(students);
        System.out.println(jsonStu);

//        List<Student> stu =JSON.parseObject(jsonStu, new TypeReference<List<Student>>(){});
        List<Student> stu =JSON.parseArray(jsonStu, Student.class);
        for(int i=0;i<stu.size();i++)
        {
            System.out.println(stu.get(i));
        }
    }
}

 

日期相关

1.日期格式化:

FastJSON可以直接对日期类型格式化,在缺省的情况下,FastJSON会将Date转成long。

例5:FastJSON将java.util.Date转成long。

1 String dateJson = JSON.toJSONString(new Date());
2         
3 System.out.println(dateJson);

输出结果:

1401370199040

 

例6:使用SerializerFeature特性格式化日期。

1 String dateJson = JSON.toJSONString(new Date(), SerializerFeature.WriteDateUseDateFormat);
2         
3 System.out.println(dateJson);

输出结果:

"2014-05-29 21:36:24"

也可以指定输出日期格式。

例7:指定输出日期格式。

1 String dateJson = JSON.toJSONStringWithDateFormat(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");
2         
3 System.out.println(dateJson);

输出结果:

"2014-05-29 21:47:00.154"

 

2.使用单引号。

例8:以例2为例。

String listJson = JSON.toJSONString(list, SerializerFeature.UseSingleQuotes);

输出结果:

[{‘key1‘:‘One‘,‘key2‘:‘Two‘},{‘key3‘:‘Three‘,‘key4‘:‘Four‘}]

3.JSON格式化。

例9:

String listJson = JSON.toJSONString(list, SerializerFeature.PrettyFormat);

输出结果:与例4结果一致。

4.输出Null字段。

 缺省情况下FastJSON不输入为值Null的字段,可以使用SerializerFeature.WriteMapNullValue使其输出。

例10:

技术分享图片
1 Map<String, Object> map = new HashMap<String,Object>();
2         
3 String b = null;
4 Integer i = 1;
5         
6 map.put("a", b);
7 map.put("b", i);
8         
9 String listJson = JSON.toJSONString(map, SerializerFeature.WriteMapNullValue);
技术分享图片

输出结果:

{"a":null,"b":1}

JSONObject,JSONArray是JSON的两个子类

JSONObject相当于Map<String, Object>,

JSONArray相当于List<Object>。

简单方法示例:

例16:将Map转成JSONObject,然后添加元素,输出。

技术分享图片
 1 Map<String, Object> map = new HashMap<String, Object>();
 2 map.put("key1", "One");
 3 map.put("key2", "Two");
 4                            
 5 JSONObject j = new JSONObject(map);
 6                    
 7 j.put("key3", "Three");
 8           
 9 System.out.println(j.get("key1"));
10 System.out.println(j.get("key2"));
11 System.out.println(j.get("key3"));
技术分享图片

输出结果:

 

1 One
2 Two
3 Three

 

 

例17:将List对象转成JSONArray,然后输出。

技术分享图片
 1 List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
 2         
 3 Map<String, Object> map = new HashMap<String, Object>();
 4 map.put("key1", "One");
 5 map.put("key2", "Two");
 6         
 7 Map<String, Object> map2 = new HashMap<String, Object>();
 8 map2.put("key1", "Three");
 9 map2.put("key2", "Four");
10         
11 list.add(map);
12 list.add(map2);
13         
14 JSONArray j = JSONArray.parseArray(JSON.toJSONString(list));
15          
16 for(int i=0; i<j.size(); i++){
17     System.out.println(j.get(i));
18 }
技术分享图片

输出结果:

1 {"key1":"One","key2":"Two"}
2 {"key1":"Three","key2":"Four"}

 

FastJson简单使用

标签:seda   for   one   日期类型   建立   stat   rgs   过滤   test   

原文地址:https://www.cnblogs.com/zailushang1996/p/8480675.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!