jackson 字符串转换成List集合的问题请教

我使用了jackson先将一个List集合对象转换成json字符串后,再将json字符串转成List集合出了问题 ,报错信息如下:
[code="java"]
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)
[/code]

我的List集合中的对象里有一个属性也是一个集合,代码如下:
HData.java
[code="java"]
package 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;
}

}

[/code]

HDataB.java:
[code="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;
}

}

[/code]

我的测试类:
[code="java"]
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 stub

    List<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 ");
    }
}

}

[/code]

JsonUtion.java:

[code="java"]

private static ObjectMapper jacksonMapper = new ObjectMapper();

public static String objectToJackson(Object obj) throws Exception {
return jacksonMapper.writeValueAsString(obj);
}
public static T jacksonToCollection(String src,Class<?> collectionClass, Class<?>... valueType)
throws Exception {
JavaType javaType= jacksonMapper.getTypeFactory().constructParametricType(collectionClass, valueType);
return (T)jacksonMapper.readValue(src, javaType);
}
[/code]

不知道jackson如何对这类集合进行转换?

2个回答

加一句即可
objectMapper.enableDefaultTyping();

huangzixun
等鱼上勾 非常感谢,好使了,加上这个后所有的转换json字符串都带有类型信息!
接近 7 年之前 回复

是需要给HDataA提供个无参数的构建器吧。

huzhian77
huzhian77 正解啊...
大约 6 年之前 回复
j_clxy
clxy大叔 我错了...
接近 7 年之前 回复
huangzixun
等鱼上勾 对,还需要一个无参的构造器!
接近 7 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问