等鱼上勾 2013-06-13 14:09
浏览 756
已采纳

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条回答 默认 最新

  • jinnianshilongnian 2013-06-13 14:13
    关注

    加一句即可
    objectMapper.enableDefaultTyping();

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB和mosek的求解问题
  • ¥20 修改中兴光猫sn的时候提示失败
  • ¥15 java大作业爬取网页
  • ¥15 怎么获取欧易的btc永续合约和交割合约的5m级的历史数据用来回测套利策略?
  • ¥15 有没有办法利用libusb读取usb设备数据
  • ¥15 为什么openeluer里面按不了python3呢?
  • ¥15 关于#matlab#的问题:训练序列与输入层维度不一样
  • ¥15 关于Ubuntu20.04.3LTS遇到的问题:在安装完CUDA驱动后,电脑会进入卡死的情况,但可以通过键盘按键进入安全重启,但重启完又会进入该情况!
  • ¥15 关于#嵌入式硬件#的问题:树莓派第一天重装配置python和opencv后第二天打开就成这样,瞎捣鼓搞出来文件夹还是没把原来的界面调回来
  • ¥20 Arduino 循迹小车程序电路出错故障求解