您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

FastJson常用的场景介绍(代码)

2024/4/28 5:23:05发布18次查看
本篇文章给大家带来的内容是关于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<student> 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<student>() {});        //转集合        string studentliststr = json.tojsonstring(studentlist);        list<student> students = json.parsearray(studentliststr, student.class);        list<student> students2 = json.parseobject(studentliststr, new typereference() {        });        //过滤字段,默认过滤null        string student3 = json.tojsonstring(student, propertyfilter);        system.out.println(student3);    }}
以上就是fastjson常用的场景介绍(代码)的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product