Reputation: 3674
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);
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
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
Guide me please what should I import to work with Jackson
. Thanks
Upvotes: 15
Views: 34548
Reputation: 3990
use these dependencies
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);
}
}
Upvotes: 15
Reputation: 48404
Looks like mixed up references.
You might be using a library that uses an old version of Jackson itself (i.e. the org.codehaus
package)...
I usually just reference Jackson through Maven.
Something like:
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>LATEST</version>
</dependency>
</dependencies>
Upvotes: 4