如何忽略json中的父标记?
这是我的json
String str = "{\"parent\": {\"a\":{\"id\": 10, \"name\":\"Foo\"}}}";
Run Code Online (Sandbox Code Playgroud)
这是从json映射的类.
public class RootWrapper {
private List<Foo> foos;
public List<Foo> getFoos() {
return foos;
}
@JsonProperty("a")
public void setFoos(List<Foo> foos) {
this.foos = foos;
}
}
Run Code Online (Sandbox Code Playgroud)
这是测试公共类JacksonTest {
@Test
public void wrapRootValue() throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String str = "{\"parent\": {\"a\":{\"id\": 10, \"name\":\"Foo\"}}}";
RootWrapper root = mapper.readValue(str, RootWrapper.class);
Assert.assertNotNull(root);
}
Run Code Online (Sandbox Code Playgroud)
我收到错误::
org.codehaus.jackson.map.JsonMappingException: Root name 'parent' does not match expected ('RootWrapper') for type [simple …
Run Code Online (Sandbox Code Playgroud) 如何将嵌套属性的忽略属性定义为 spring BeanUtils?
BeanUtils.copyProperties(source, target, ignorePropertiesName);
Run Code Online (Sandbox Code Playgroud)
我有嵌套类名称的类 Contact 及其两个属性“firstName”和“lastname”。
我尝试了三种不同的模式来传递嵌套的文件名以忽略列表,但它们都不起作用。
"contact.name.lastName"
"name.lastName"
"lastName"
Run Code Online (Sandbox Code Playgroud)
下面是带有类定义的完整单元测试,我使用的是 java 8 的 spring-beans-4.3.9.RELEASE 版本。
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.BeanUtils;
public class NestedCopyPropertiesTest {
// @Test
public void CopyProperties() {
String firstName = "Andy";
String lastName = "Murray";
Contact source = new Contact(new Name(firstName, lastName));
Contact target = new Contact(new Name(null, null));
BeanUtils.copyProperties(source, target);
String targetFirstName = target.getName().getFirstName();
String targetLastName = target.getName().getLastName();
log("targetFirstName: " + targetFirstName);
log("targetLastName: " + targetLastName);
Assert.assertTrue("Failed to copy nested properties.", …
Run Code Online (Sandbox Code Playgroud)