Java 用Jackson进行json和object之间的转换(并解决json中存在新增多余字段的问题)
1、添加jackson库
如果是maven工程,需要在pom.xml中添加jackson的依赖:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.3</version>
</dependency>
2、json转object
ObjectMapper objectMapper = new ObjectMapper();
YourClass class = objectMapper.readValue(YourJson, YourClass.class);
如果json中有新增的字段并且是YourClass类中不存在的,则会转换错误
1)需要加上如下语句:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
YourClass class = objectMapper.readValue(YourJson, YourClass.class);
2)jackson库还提供了注解方法,用在class级别上:
这种方法的好处是不用改变要转化的类,即本例的YourClass。(如果YourClass不是你维护的,或者不可修改的,可以用这个方法)
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class YourClass {
...
}
3、object转json
本例中是YourClass对象需要转化为json:
import com.fasterxml.jackson.databind.ObjectMapper;
ObjectMapper objectMapper = new ObjectMapper();
YourClass yourClass = new YourClass();
String json = objectMapper.writeValueAsString(yourClass);