分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
通常都是通过第二篇介绍的Full Data Binding技术迅速享受Jackson带来的方便快捷。但是又很快就碰到一些小的问题,
如何忽略某些属性,不让它参与序列化。(请别误会,这里不是谈Java serializable机制)
还是蛮简单的,一个Annotation而已。修改一下Response.java文件,在message属性上加上@JsonIgnore
import com.fasterxml.jackson.annotation.JsonIgnore;import lombok.Getter;import lombok.Setter;/** * * @author chenshu */public class Response { @Getter @Setter private String status; @JsonIgnore @Getter @Setter private String message;}
App.java代码如下:
/** * Hello world! * */public class App { public static void main( String[] args ) throws IOException { ObjectMapper mapper = new ObjectMapper(); // create once, reuse String jsonSource = "{\"message\":\"Login succeeded!\",\"status\":0}"; Response response = mapper.readValue(jsonSource, Response.class); String result = mapper.writeValueAsString(response); }}
结果是response对象的message字段为null,再序列化成字符串result后,只包含了status。
如果想修改属性名称(准确的描述是修改属性的getXX方法的名称),用下面的Annotation:
@JsonProperty("newName")
具体参考:https://github.com/FasterXML/jackson-databind
很快又有需求来了。如果一个类的属性我在一种情况下需要序列化几个到JSON对象,然后在另一种情况下又需要序列化另外几个到JSON对象。上面的方式就不行了。
具体可以参考下面的文档JSON Filter
http://wiki.fasterxml.com/JacksonFeatureJsonFilter