依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>
SerializerFeature
public static void main(String[] args) {
@Data
@Accessors
@AllArgsConstructor
class Xsd{
private Integer id;
private String name;
private Date brDate;
private String clazz;
private Integer size;
private List list;
}
Xsd xsd=new Xsd(1,"\"张林\"",new Date(),null,null,null);
System.out.println("===========================");
System.out.println(JSON.toJSONString(xsd));
System.out.println("显示为null的字段===========================");
System.out.println(JSON.toJSONString(xsd,SerializerFeature.WriteMapNullValue));
System.out.println("null显示''===========================");
System.out.println(JSON.toJSONString(xsd,SerializerFeature.WriteNullStringAsEmpty));
System.out.println("转换array===========================");
System.out.println(JSON.toJSONString(xsd,SerializerFeature.BeanToArray));
System.out.println("全局修改日期格式,默认为false===========================");
JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd";
System.out.println(JSON.toJSONString(xsd,SerializerFeature.WriteDateUseDateFormat));
System.out.println("PrettyFormat格式化json===========================");
System.out.println(JSON.toJSONString(xsd,SerializerFeature.PrettyFormat));
System.out.println("按字段名称排序后输出===========================");
System.out.println(JSON.toJSONString(xsd,SerializerFeature.SortField));
System.out.println("Date使用ISO8601格式输出===========================");
System.out.println(JSON.toJSONString(xsd, SerializerFeature.UseISO8601DateFormat));
System.out.println("单引号UseSingleQuotes=====双引号QuoteFieldNames========");
System.out.println(JSON.toJSONString(xsd, SerializerFeature.UseSingleQuotes));
System.out.println("数字为null输出0========");
System.out.println(JSON.toJSONString(xsd, SerializerFeature.WriteNullNumberAsZero));
System.out.println("中文转义支持IE6========");
System.out.println(JSON.toJSONString(xsd, SerializerFeature.BrowserCompatible));
System.out.println("特殊字符转义========");
System.out.println(JSON.toJSONString(xsd, SerializerFeature.DisableCheckSpecialChar));
System.out.println("list为null转[]========");
System.out.println(JSON.toJSONString(xsd, SerializerFeature.WriteNullListAsEmpty));
}
@JSONField
注意:1、若属性是私有的,必须有set*方法。否则无法反序列化。
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.Date;
import java.util.List;
@Data
@Accessors
public class TestCor {
/**
* int ordinal() default 0 字段排序
* String name() default ""; 重命名
* String format() default ""; 格式化
* boolean serialize() default true; 是否序列化
* boolean deserialize() default true;是否反序列化
* SerializerFeature[] serialzeFeatures() default {}; 序列化枚举
* Feature[] parseFeatures() default {}; 控制反序列化的一些规则
* String label() default ""; 批量处理某一类的属性,比如不序列化某一类属性。
* boolean jsonDirect() default false; 可直接赋值为JSON xxx="{'size':1}"
* Class<?> serializeUsing() default Void.class; 定制序列化
* Class<?> deserializeUsing() default Void.class; 定制反序列化
* String[] alternateNames() default {}; 反序列化时使用多个不同的字段名称 AJSON{"AField":1} BJSON{"BField":2}
* alternateNames={"AField","BField"} 那么2个key都会相应的赋值到当前字段
* boolean unwrapped() default false; 把子对象的内容放到父对象里面
* String defaultValue() default ""; 默认值
*/
@JSONField(ordinal = 3,label = "b")
private Integer id=5;
@JSONField(ordinal = 2,name = "othername",label = "a",alternateNames={"othername"})
private String name="张";
@JSONField(ordinal = 1,format = "yyyy-MM-dd",label = "a",deserialize =false)
private Date format=new Date();
@JSONField(ordinal = 4,jsonDirect = true)
private String jsonDirect="{'size':1}";
@JSONField(ordinal = 5,serializeUsing = NumberCon.class,label = "b",deserializeUsing =NumberConUn.class )
private Double hight=175.2;
@JSONField(ordinal = 6,serialzeFeatures = SerializerFeature.WriteNullListAsEmpty)
private List list=null;
@JSONField(ordinal = 7,defaultValue = "--")
private String defaultValue;
@JSONField(ordinal = 9,unwrapped = false)
private Objs testCor2=new Objs();
@JSONField(unwrapped = true)
private Objs testCor1=new Objs();
@Data
@Accessors
class Objs{
private String childObj="childObj";
}
public static void main(String[] args) {
TestCor t=new TestCor();
System.out.println(JSON.toJSONString(t));
String str="{\"childObj\":\"childObj\",\"format\":\"2021-06-29\",\"othername\":\"张\",\"id\":5,\"jsonDirect\":{\"size\":1},\"hight\":\"175.2CM\",\"list\":[],\"defaultValue\":\"--\",\"testCor2\":{\"childObj\":\"childObj\"}}";
TestCor testCor = JSONObject.parseObject(str, TestCor.class);
System.out.println(JSON.toJSONString(testCor,SerializerFeature.PrettyFormat));
}
public class DecimalUtil extends JsonSerializer<BigDecimal> {
@Override
public void serialize(BigDecimal bigDecimal, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeString(bigDecimal.setScale(2, RoundingMode.HALF_UP).toString());
// jsonGenerator.writeNumber(bigDecimal.setScale(2));
}
}
JSONPath
public static void main(String[] args) {
String str="{\"asd\":[{\"id\":1,\"name\":\"小明\"},{\"id\":2,\"name\":\"张3\"}]}";
String str2="[{\"id\":1,\"name\":\"小明\"},{\"id\":2,\"name\":\"张3\"},{\"id\":3,\"name\":null}]";
JSONObject json1=JSONObject.parseObject(str);
JSONArray json2=JSONArray.parseArray(str2);
//数组全部对象
System.out.println(JSONPath.eval(json1, "$"));
//访问数组
System.out.println(JSONPath.eval(json2, "$"));
//访问数组第一个
System.out.println(JSONPath.eval(json2, "$[0]"));
//访问数组第一个的id
System.out.println(JSONPath.eval(json2, "$[0].id"));
//访问数组下标0-1的id
System.out.println(JSONPath.eval(json2, "$[0,1].id"));
//访问对象非空过滤
System.out.println(JSONPath.eval(json2, "$[?(name)]"));
//过滤id>1
System.out.print(JSONPath.eval(json2, "$[id>1]"));
System.out.println(JSONPath.eval(json1, "$.asd[id>1]"));
//过滤比较
System.out.print(JSONPath.eval(json2, "$[name='张3']"));
System.out.println(JSONPath.eval(json1, "$.asd[name='张3']"));
//字符匹配
System.out.print(JSONPath.eval(json2, "$[name like '%张%']"));
System.out.println(JSONPath.eval(json1, "$.asd[name='张3']"));
//in 包含
System.out.print(JSONPath.eval(json2, "$[name in ('张三','张3')]"));
System.out.println(JSONPath.eval(json1, "$.asd[name in ('张三','张3')]"));
// 数组长度
System.out.println(JSONPath.eval(json1, "$.asd.size()"));
}