fastjson支持以多种方式定制顺序正确的棘球蚴。
@JSONField定制顺序合适的棘球蚴
@用@JSONType定制顺序合适的金针菇
用序列化过滤器自定义有序蘑菇
在ParseProcess中定制相反顺序的适当蘑菇
使用@JSONField :
使用@JSONField的格式配置日期格式。 这在上一篇文章中展示了。
利用@JSONField的serialize,指定字段顺序不合适的棘球蚴:
package com.ant.jdk8.json;
import com.Alibaba.fast JSON.annotation.JSON field;
import java.util.Date;
公共类用户{
私有字符串名称;
@ JSON field (序列=假) ) ) ) ) ) ) ) )。
私密int age;
@ JSON field (格式=' yyyy-mm-ddhh : mm : ss ' () ) ) ) ) ) ) 652 )
私密日期隐藏;
公共字符串getname
返回名称;
}
公共语音集名称(字符串名称) {
this.name=name;
}
公共int get age () {
返回age;
}
公共语音设置(intage ) {
this.age=age;
}
公共日期获取的湿润杯子() () () )。
返回隐藏;
}
公共语音集的湿润杯子(Date hireDate ) )。
this.hireDate=hireDate;
}
公共用户(字符串名称、int age、Date hireDate ) {
this.name=name;
this.age=age;
this.hireDate=hireDate;
}
公共用户() }
}
}
package com.ant.jdk8.json;
import com.Alibaba.fast JSON.JSON;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
公共类JSON demo {
publicstaticvoidmain (stringhxsdxgargs ) {
convertCollectionToJson (;
}
publicstaticvoidconvertcollectiontojson (
列表用户列表=new ArrayList (;
Userlist.add(newuser('Tom ',20,new Date ) );
userlist.add (新用户(' jack ',25,新日期) );
stringjsonstring=JSON.tojsonstring (userlist );
}
}
利用@JSONField的ordinal,指定顺序合适的棘球绦虫顺序。 (此特性需要1.1.42或更高版本)。
package com.ant.jdk8.json;
import com.Alibaba.fast JSON.annotation.JSON field;
import java.util.Date;
公共类用户{
@JSONfield(ordinal=3) ) ) ) ) ) ) ) ) )。
私有字符串名称;
@JSONfield(ordinal=2) ) ) ) ) ) ) ) )。
私密int age;
@ JSON field (格式=' yyyy-mm-ddhh : mm : ss ',ordinal=1) ) ) ) ) ) ) ) )。
私密日期隐藏;
公共字符串getname
返回名称;
}
公共语音集名称(字符串名称) {
this.name=name;
}
公共int get age () {
返回age;
}
公共语音设置(intage ) {
this.age=age;
}
公共日期获取的湿润杯子() () () )。
返回隐藏;
}
公共语音集的湿润杯子(Date hireDate ) )。
this.hireDate=hireDate;
}
公共用户(字符串名称、int age、Date hireDate ) {
this.name=name;
this.age=age;
this.hireDate=hireDate;
}
公共用户() }
}
}
package com.ant.jdk8.json;
import com.Alibaba.fast JSON.JSON;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
公共类JSON demo {
publicstaticvoidmain (stringhxsdxgargs ) {
convertCollectionToJson (;
}
publicstaticvoidconvertcollectiontojson (
列表用户列表=new ArrayList (;
Userlist.add(newuser('Tom ',20,new Date ) );
userlist.add (新用户(' jack ',25,新日期) );
stringjsonstring=JSON.tojsonstring (userlist );
}
}
在@JSONField的name中指定字段的名称:
package com.ant.jdk8.json;
import com.Alibaba.fast JSON.annotation.JSON field;
import java.util.Date;
公共类用户{
@JSONfield(ordinal=3,name='username ' ) ) ) ) ) ) ) )。
私有字符串名称;
@JSONfield(ordinal=2) ) ) ) ) ) ) ) )。
私密int age;
@ JSON field (格式=' yyyy-mm-ddhh : mm : ss ',ordinal=1) ) ) ) ) ) ) ) )。
私密日期隐藏;
公共字符串getname
返回名称;
}
公共语音集名称(字符串名称) {
this.name=name;
}
公共int get age () {
返回age;
}
公共语音设置(intage ) {
this.age=age;
}
公共日期获取的湿润杯子() () () )。
返回隐藏;
}
公共语音集的湿润杯子(Date hireDate ) ) )。
this.hireDate=hireDate;
}
公共用户(字符串名称、int age、Date hireDate ) {
this.name=name;
this.age=age;
this.hireDate=hireDate;
}
公共用户() }
}
}
package com.ant.jdk8.json;
import com.Alibaba.fast JSON.JSON;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
公共类JSON demo {
publicstaticvoidmain (stringhxsdxgargs ) {
convertCollectionToJson (;
}
publicstaticvoidconvertcollectiontojson (
列表用户列表=new ArrayList (;
Userlist.add(newuser('Tom ',20,new Date ) );
userlist.add (新用户(' jack ',25,新日期) );
stringjsonstring=JSON.tojsonstring (userlist );
}
}