本文介绍了不能与杰克逊合作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很奇怪,为什么没有一个决心与杰克逊的工作方式。我只是想解析 JSON 字符串:

I am wondering why there is not a determined way to work with Jackson. I just want to parse JSON string:

ObjectMapper mapper = new ObjectMapper();
Customer[] myObjects = mapper.readValue(file, Customer[].class);

但我真糊涂我应该怎么导入做到这一点。根据本<一href=\"http://examples.java$c$cgeeks.com/core-java/json/jackson/convert-java-object-to-from-json-using-jackson-example/\"相对=nofollow>链接,我试图导入映射-asl.jar 。但是,我得到这个编译错误:

But I really confused what should I import to do that. According to this link, I tried to import mapper-asl.jar. But I get this compile error:

The type org.codehaus.jackson.JsonParser cannot be resolved. It is indirectly referenced from required .class files

然后我尝试导入杰克逊核心-2.4.2 杰克逊数据绑定-2.4.2 。所以没有编译错误,但我得到这个运行时异常,而不是(在映射定义行):

Then I try to import jackson-core-2.4.2 and jackson-databind-2.4.2. So there was no compile error but I got this runtime exception instead (in mapper definition line):

java.lang.NoClassDefFoundError: com.fasterxml.jackson.annotation.JsonAutoDetect

指引我,请我应该怎么导入使用杰克逊工作。谢谢

推荐答案

使用这些依赖




public class JsonTest {
    public static void main(String[] args) throws JsonProcessingException {
    ObjectMapper mapper=new ObjectMapper();
    Map<String,String> dt=new Hashtable();
    dt.put("1", "welcome");
    dt.put("2", "bye");
    String jsonString = mapper.writeValueAsString(dt)
    System.out.println(jsonString);
    }
}

尝试,让我知道,如果它的工作原理。

Try and let me know if it works.

这篇关于不能与杰克逊合作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-27 17:48