我想知道为什么没有确定的方法来处理 Jackson
。我只想解析 JSON
字符串:
ObjectMapper mapper = new ObjectMapper();
Customer[] myObjects = mapper.readValue(file, Customer[].class);
但我真的很困惑我应该导入什么来做到这一点。根据这个link ,我尝试导入 mapper-asl.jar
。但是我得到了这个编译错误:
The type org.codehaus.jackson.JsonParser cannot be resolved. It is indirectly referenced from required .class files
然后我尝试导入 jackson-core-2.4.2
和 jackson-databind-2.4.2
。所以没有编译错误,但我得到了这个运行时异常(在映射器定义行中):
java.lang.NoClassDefFoundError: com.fasterxml.jackson.annotation.JsonAutoDetect
请指导我应该导入什么以使用 Jackson
。谢谢
最佳答案
使用这些依赖
jackson-databind
jackson-annotations
jackson-core
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);
}
}
关于java - 无法与 jackson 合作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25947238/