11

我无法让 JSON 正常工作。ObjectMapper无法解决。库已正确导入。

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();
        }
    }
}
4

2 回答 2

12
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
于 2017-01-06T07:53:53.607 回答
7

添加import com.fasterxml.jackson.databind.ObjectMapper;到您的项目中。

于 2019-03-08T11:44:50.380 回答