Java – cannot work with Jackson

I wonder why there is no definite way to work with Jackson I just want to parse JSON strings:

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

But I'm really confused about what I should do to do this Based on this link, I tried to import mapper ASL jar. But I got this compilation error:

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

Then I tried to import jackson-core-2.4 2 and jackson-databind-2.4 2. So there is no compilation error, but I get this runtime exception (in the mapper definition line):

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

Guide me on what I should enter to work with Jackson thank you

Solution

Use these dependencies

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 to let me know if it works

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>