public class Parent {
private List<Child> childs;
}
public class Parent {
private Set<Child> childs;
}
@RequestMapping(value = "/save", method = RequestMethod.POST)
public void saveFooter(@RequestBody Parent parent, Model model)
下面的是示例JSON对象。
{child: [{ name: 'a'}, { name: 'b'}]}
当我在Parent.class parent.getChilds.size()
中对子项使用设置时,子项的大小仅为1(这是错误的)。
但是当我在Parent.class parent.getChilds.size()
中使用列表作为子项时,子项的大小为2(这是正确的)
编辑:
public class Child {
private String name;
}
jackson maven依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate4</artifactId>
<version>2.4</version>
</dependency>
编辑:
public class Child {
private Long id;
private String name;
@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Child other = (Child) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
发现问题是由于我实现了Child.class的equals方法 这使得两个物体相等。
答案 0 :(得分:0)
<强> Parent.java 强>
package com.test;
import java.util.Set;
public class Parent {
private Set<Child> childs;
public Set<Child> getChilds() {
return childs;
}
public void setChilds(Set<Child> childs) {
this.childs = childs;
}
}
<强> Child.Java 强>
package com.test;
public class Child {
private String name;
public Child(String name) {
this.name = name;
}
public Child() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<强> Test.java 强>
package com.test;
import java.io.IOException;
import java.util.HashSet;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import flexjson.JSONSerializer;
public class MvelCompilation {
public static void main(String[] args) {
Parent parent = new Parent();
Child c1 = new Child("Ankur1");
Child c2 = new Child("Ankur2");
HashSet<Child> childSet = new HashSet<Child>();
childSet.add(c1);
childSet.add(c2);
parent.setChilds(childSet);
JSONSerializer serializer = new JSONSerializer();
System.out.println("Json String is = " + serializer.exclude("*.class").deepSerialize(parent));
String s = "{\"childs\":[{\"name\":\"Ankur1\"},{\"name\":\"Ankur2\"}]}";
try {
Parent parent1 = new ObjectMapper().readValue(s, Parent.class);
System.out.println("size = " + parent1.getChilds().size());
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
<强>输出强>
Json String = {“childs”:[{“name”:“Ankur1”},{“name”:“Ankur2”}}}
size = 2