标签:plain sort 详解 eval ppi 过滤 default number creat
public static void main(String[] args) {
AutoPartsSearchRequest request = new AutoPartsSearchRequest();
request.setKeywords("123");
request.setSortingField("234242");
String str = JSONObject.toJSONString(request);//fastjson默认转换是不序列化null值对应的key的
System.out.println(str);
}
QuoteFieldNames———-输出key时是否使用双引号,默认为true
WriteMapNullValue——–是否输出值为null的字段,默认为false
WriteNullNumberAsZero—-数值字段如果为null,输出为0,而非null
WriteNullListAsEmpty—–List字段如果为null,输出为[],而非null
WriteNullStringAsEmpty—字符类型字段如果为null,输出为”“,而非null
WriteNullBooleanAsFalse–Boolean字段如果为null,输出为false,而非null
public static void main(String[] args) {
AutoPartsSearchRequest request = new AutoPartsSearchRequest();
request.setKeywords("123");
request.setSortingField("234242");
String str = JSONObject.toJSONString(request, SerializerFeature.WriteMapNullValue);
System.out.println(str);
}
public static void main(String[] args) {
AutoPartsSearchRequest request = new AutoPartsSearchRequest();
request.setKeywords("123");
request.setSortingField("234242");
String str = JSONObject.toJSONString(request, SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullStringAsEmpty);
System.out.println(str);
}
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
AutoPartsSearchRequest request = new AutoPartsSearchRequest();
request.setKeywords("123");
request.setSortingField("234242");
ObjectMapper mapper = new ObjectMapper();
String str = mapper.writeValueAsString(request);
System.out.println(str);
//输出结果(此处就不格式化了):{"sortingField":"234242","partsClassifyId":null,"partsSubClassifyId":null,"sortingDirection":null:......
}
1.实体上
@JsonInclude(Include.NON_NULL)
//将该标记放在属性上,如果该属性为NULL则不参与序列化
//如果放在类上边,那对这个类的全部属性起作用
//Include.Include.ALWAYS 默认
//Include.NON_DEFAULT 属性为默认值不序列化
//Include.NON_EMPTY 属性为 空(“”) 或者为 NULL 都不序列化
//Include.NON_NULL 属性为NULL 不序列化
2.代码上
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Include.NON_NULL);
//通过该方法对mapper对象进行设置,所有序列化的对象都将按改规则进行系列化
//Include.Include.ALWAYS 默认
//Include.NON_DEFAULT 属性为默认值不序列化
//Include.NON_EMPTY 属性为 空(“”) 或者为 NULL 都不序列化
//Include.NON_NULL 属性为NULL 不序列化
注意:只对VO起作用,Map List不起作用,另外jackson还能过滤掉你设置的属性,具体的就各位自己去研究源码了
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
AutoPartsSearchRequest request = new AutoPartsSearchRequest();
request.setKeywords("123");
request.setSortingField("234242");
Gson g = new GsonBuilder().create();
String str = g.toJson(request);
System.out.println(str);
//输出结果:{"sortingField":"234242","keywords":"123"}
}
Gson g = new GsonBuilder().serializeNulls().create();
FastJson、Jackson、Gson进行Java对象转换Json的细节处理
标签:plain sort 详解 eval ppi 过滤 default number creat
原文地址:http://www.cnblogs.com/GarfieldEr007/p/6822316.html