ObjectMapper cannot be resolved to a type

Jürgen K. picture Jürgen K. · Dec 15, 2015 · Viewed 29.3k times · Source

I have troubles getting JSON to work. ObjectMapper cannot be resolved. Library is imported correctly.

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONException;
import net.sf.json.util.*;

import com.fasterxml.jackson.*;

public class Json {
    private static final String jsonFilePath = "C:\\Users\\Juergen\\Desktop\\filesForExamples\\mapExample.json";

    public static void objectToJSON(HashMap<String, Mat> map) {
        //Map<String, Object> mapObject = new HashMap<String, Object>();
        ObjectMapper mapper = new ObjectMapper();

        try {
            objectMapper.writeValue(new File(jsonFilePath), map);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Answer

Rohit Yadav picture Rohit Yadav · Jan 6, 2017
If you are using **maven** project then add the following in the `POM.xml`

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.12.1</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.12.1</version>
</dependency>
    
But if you are using a **simple java** project then you need to add the following jars in your class path:
    
    jackson-core-2.1.X,
    jackson-databind-2.1.X