huangzixun
等鱼上勾
2013-06-13 14:09

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

  • jackson
已采纳

我使用了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条回答

  • jinnianshilongnian jinnianshilongnian 8年前

    加一句即可
    objectMapper.enableDefaultTyping();

    点赞 评论 复制链接分享
  • j_clxy clxy大叔 8年前

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

    点赞 评论 复制链接分享