這篇文章主要介紹了FastJson常用的場景有哪些,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。
企業(yè)建站必須是能夠以充分展現(xiàn)企業(yè)形象為主要目的,是企業(yè)文化與產(chǎn)品對外擴展宣傳的重要窗口,一個合格的網(wǎng)站不僅僅能為公司帶來巨大的互聯(lián)網(wǎng)上的收集和信息發(fā)布平臺,創(chuàng)新互聯(lián)面向各種領域:成都攪拌罐車等成都網(wǎng)站設計、營銷型網(wǎng)站解決方案、網(wǎng)站設計等建站排名服務。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 ListstudentList; 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); //轉(zhuǎn)對象 Student student1 = JSON.parseObject(studentStr, Student.class); Student student2 = JSON.parseObject(studentStr, new TypeReference () {}); //轉(zhuǎn)集合 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常用的場景有哪些內(nèi)容對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,遇到問題就找創(chuàng)新互聯(lián),詳細的解決方法等著你來學習!