问题描述
我使用了jackson先将一个List集合对象转换成json字符串后,再将json字符串转成List集合出了问题 ,报错信息如下:Exception in thread "main" org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class com.mailbill.HDataA]: can not instantiate from JSON object (need to add/enable type information?) at [Source: java.io.StringReader@147917a; line: 1, column: 3]at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObjectUsingNonDefault(BeanDeserializer.java:746)at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:683)at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217)at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:194)at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:30)at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2732)at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1877)at com.mailbill.datacore.util.JsonUtil.jacksonToCollection(JsonUtil.java:315)at com.mailbill.TestJson.main(TestJson.java:27)我的List集合中的对象里有一个属性也是一个集合,代码如下:HData.javapackage com.mailbill;import java.util.List;public class HDataA {private String name;private String code;private List<HDataB> dataBList ;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getCode() {return code;}public void setCode(String code) {this.code = code;}public List<HDataB> getDataBList() {return dataBList;}public void setDataBList(List<HDataB> dataBList) {this.dataBList = dataBList;}public HDataA(String name, String code, List<HDataB> dataBList) {super();this.name = name;this.code = code;this.dataBList = dataBList;}}HDataB.java:package com.mailbill;public class HDataB {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public HDataB(String name) {super();this.name = name;}}我的测试类:package com.mailbill;import java.util.ArrayList;import java.util.List;import org.aspectj.apache.bcel.generic.ARRAYLENGTH;import com.mailbill.datacore.util.JsonUtil;public class TestJson {/** * @param args * @throws Exception */public static void main(String[] args) throws Exception {// TODO Auto-generated method stubList<HDataA> dataAlist = new ArrayList<HDataA>();List<HDataB> dataBList = new ArrayList<HDataB>();dataBList.add(new HDataB("hehe"));dataBList.add(new HDataB("xixi"));dataAlist.add(new HDataA("nidaye","woqu", dataBList));String str = JsonUtil.objectToJackson(dataAlist);System.out.println(str);dataAlist = JsonUtil.jacksonToCollection(str, List.class, HDataA.class);if(dataAlist ==null){System.out.println(" dataAlist is null");}else{System.out.println(" ok ");}}}JsonUtion.java:private static ObjectMapper jacksonMapper = new ObjectMapper();public static String objectToJackson(Object obj) throws Exception {return jacksonMapper.writeValueAsString(obj);}public static <T> T jacksonToCollection(String src,Class<?> collectionClass, Class<?>... valueType)throws Exception {JavaType javaType= jacksonMapper.getTypeFactory().constructParametricType(collectionClass, valueType); return (T)jacksonMapper.readValue(src, javaType);}不知道jackson如何对这类集合进行转换?
解决方案
加一句即可 objectMapper.enableDefaultTyping();
解决方案二:
是需要给HDataA提供个无参数的构建器吧。