12

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();
        }
    }
}
Jürgen K.
  • 3,427
  • 9
  • 30
  • 66
  • 1
    Importing `ObjectMapper` would be a really good idea. – Tom Dec 15 '15 at 13:49
  • yeah, but shouldnt eclipse give me the proposal? – Jürgen K. Dec 15 '15 at 13:52
  • 1
    If Eclipse has something like a "quick fix", then it would could suggest an import. But I don't know if it has such a feature. But anyway, this problem is the missing import, so you could delete this question, because it won't help future readers and asking, why Eclipse doesn't suggest an import before noting the error, is quite off-topic :P. – Tom Dec 15 '15 at 13:53
  • This is maybe something what you're looking for: https://wiki.eclipse.org/FAQ_What_is_a_Quick_Fix%3F – Tom Dec 15 '15 at 13:56
  • i found out that i need to import the jackson package additionally. i downloaded it. But how do i get the full path to know for a correct package import? – Jürgen K. Dec 15 '15 at 14:07
  • Depends on your version. Check the JavaDoc of your version or use Ctrl+Space for auto completion. Eclipse should then show available classes for import. – Tom Dec 15 '15 at 14:12
  • jar file without java doc, auto completion not helping – Jürgen K. Dec 15 '15 at 14:14
  • You don't need the JavaDoc in the jar, since it should be available on the internet :P. – Tom Dec 15 '15 at 14:19
  • what has any javadoc to do with the downloaded package name?sorry i'm not sure what you mean – Jürgen K. Dec 15 '15 at 14:20
  • 1
    It contains the full qualified name. – Tom Dec 15 '15 at 14:20
  • if u want to auto import needed objects in eclipse, use the shortcut ctrl + shift + o.. – lunatikz Dec 15 '15 at 14:24
  • if u just downloaded Jackson as jar file, dont forget to add it to your buildpath – lunatikz Dec 15 '15 at 14:25
  • added, still not found the mapper – Jürgen K. Dec 15 '15 at 14:32
  • did u refreshed your project ? (if you just copied the jar file in a folder like /lib inside your project, eclipse wont recognize, until you refresh) and maybe should clean and recompile.. – lunatikz Dec 15 '15 at 14:34
  • refreshed!But the jackson packages doesn't contain "databind" – Jürgen K. Dec 15 '15 at 14:36
  • 3
    and make sure, that you have the jar file "..jackson-databind..", for using ObjectMapper .. – lunatikz Dec 15 '15 at 14:36
  • 4
    yeah there are several packages of jackson, for using ObjectMapper u need to get jackson-databind. So you have for jackson atleast 2 jar files.. jackson-core and jackson-databind – lunatikz Dec 15 '15 at 14:37
  • Lunatikz you re right, thanks – Jürgen K. Dec 15 '15 at 14:43
  • it's a shame that for serialization of generics you need 3 packages – Jürgen K. Dec 15 '15 at 14:43
  • u r welcome :) If you like, u can upvote my comment ;) – lunatikz Dec 15 '15 at 14:46

2 Answers2

14
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
Fatima Naik
  • 304
  • 3
  • 11
Rohit Yadav
  • 2,252
  • 16
  • 18
9

Add import com.fasterxml.jackson.databind.ObjectMapper; to your project.

Vishwa Ratna
  • 5,567
  • 5
  • 33
  • 55