0

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)

Dilshad Rana
  • 148
  • 9
  • Line 18 gives problem which is: car = objectMapper.readValue(carJson, Car.class); – Dilshad Rana Sep 21 '15 at 18:01
  • 1
    You have inconsistent Jackson library versions. – Sotirios Delimanolis Sep 21 '15 at 18:04
  • What does that mean in plain English? I want help from someone who has patience and time to properly guide me. I don't know what do you mean by "inconsistent libraries". I downloaded jackson-annotatins-2.6.0.jar, jackson-core.2.5.0.jar, and jackson-databind-2.6.0.jar and added them to the libraries in netbeans. – Dilshad Rana Sep 21 '15 at 18:36
  • 2.6.0 vs 2.5.0. Do you see a difference? Do you see something that is not consistent? – Sotirios Delimanolis Sep 21 '15 at 18:37
  • Yes I do but jackson-core.2.5.0.jar is the latest version that is available and the others have version 2.6.0. Now I tried to downlod all of versio 2.5.0 but the page for jackson-databind is not available version 2.5.0. The version available is 2.5.4 and even that link gives a 404 Not Found error. Any how I am trying to find it on some other website. – Dilshad Rana Sep 21 '15 at 18:43
  • http://mvnrepository.com/artifact/com.fasterxml.jackson.core – Sotirios Delimanolis Sep 21 '15 at 18:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90258/discussion-between-ajmal-khan-and-sotirios-delimanolis). – Dilshad Rana Sep 21 '15 at 19:06

0 Answers0