FastJson常用的场景有哪些-创新互联

这篇文章主要介绍了FastJson常用的场景有哪些,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获。下面让小编带着大家一起了解一下。

企业建站必须是能够以充分展现企业形象为主要目的,是企业文化与产品对外扩展宣传的重要窗口,一个合格的网站不仅仅能为公司带来巨大的互联网上的收集和信息发布平台,创新互联面向各种领域:成都搅拌罐车成都网站设计营销型网站解决方案、网站设计等建站排名服务。

JavaBean

package com.daily.json;

import com.alibaba.fastjson.annotation.JSONField;

import java.util.Date;

public class Student {

    @JSONField(name = "NAME", ordinal = 3)
    private String name;
    @JSONField(ordinal = 2)
    private int age;
    @JSONField(format = "yyyy-MM-dd HH:mm:ss", ordinal = 1)
    private Date birthDay;
    @JSONField(serialize = false)
    private String addr;

    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;
    }

    public Date getBirthDay() {
        return birthDay;
    }

    public void setBirthDay(Date birthDay) {
        this.birthDay = birthDay;
    }

    public String getAddr() {
        return addr;
    }

    public void setAddr(String addr) {
        this.addr = addr;
    }
}

测试类

package com.daily.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.PropertyFilter;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class TestFastJson {

    private static Student student;
    private static List studentList;
    static {
        student = new Student();
        student.setName("张三");
        student.setAge(18);
        student.setBirthDay(new Date());
        student.setAddr("湖南");

        studentList = new ArrayList<>();
        studentList.add(student);
        studentList.add(student);
    }

    private static PropertyFilter propertyFilter = (object, name, value) -> {
        if (name.equals("age") && value.equals(18)) {
                return false;
        }
        return true;
    };

    public static void main(String[] args) {
        String studentStr = JSON.toJSONString(student);
        //转对象
        Student student1 = JSON.parseObject(studentStr, Student.class);
        Student student2 = JSON.parseObject(studentStr, new TypeReference() {});
        //转集合
        String studentListStr = JSON.toJSONString(studentList);
        List students = JSON.parseArray(studentListStr, Student.class);
        List students2 = JSON.parseObject(studentListStr, new TypeReference>() {
        });
        //过滤字段,默认过滤null
        String student3 = JSON.toJSONString(student, propertyFilter);
        System.out.println(student3);
    }
}

感谢你能够认真阅读完这篇文章,希望小编分享FastJson常用的场景有哪些内容对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,遇到问题就找创新互联,详细的解决方法等着你来学习!


网页标题:FastJson常用的场景有哪些-创新互联
新闻来源:http://scyanting.com/article/dgidsp.html