I followed the tutorial on http://tutorials.jenkov.com/java-json/jackson-objectmapper.html and there is no error in the syntex. There seems to be no problem in the code and I have added the JACKSON jar files to the library also in Netbeans using the standard method of adding a library to a Netbeans project. Still I am unable to convert the JSON string to the java object. Here is my code.
class Car {
private int doors;
private String brand;
public int getDoors() {
return doors;
}
public void setDoors(int doors) {
this.doors = doors;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
void printCar() {
System.out.println("car.brand = " + getBrand());
System.out.println("car.doors = " + getDoors());
}
}//class Car ends here.
Here is the code using method readValue of class ObjectMapper
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ObjectMapperExampleWithString1 {
public static void main(String args[]) {
ObjectMapper objectMapper = new ObjectMapper();
String carJson
= "{ \"brand\" : \"Mercedes\", \"doors\" : 5 }";
Car car = null;
try {
car = objectMapper.readValue(carJson, Car.class);
} catch (IOException ex) {
Logger.getLogger(ObjectMapperExampleWithString1.class.getName()).log(Level.SEVERE, null, ex);
}
if (car != null){
car.printCar();
}else{
System.out.println("Object car of type Car is null. Problem!!!");
}
}
}
And here is the exception that I get.
run: Exception in thread "main" java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.JavaType.isReferenceType()Z at com.fasterxml.jackson.databind.deser.BasicDeserializerFactory.findDefaultDeserializer(BasicDeserializerFactory.java:1522) at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.findStdDeserializer(BeanDeserializerFactory.java:167) at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:132) at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer2(DeserializerCache.java:403) at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer(DeserializerCache.java:352) at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:264) at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244) at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142) at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:461) at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:3804) at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3698) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2714) at myJackson.ObjectMapperExampleWithString1.main(ObjectMapperExampleWithString1.java:18) Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds)