杰克逊反序列化基本类型

huangapple 未分类评论46阅读模式
英文:

jackson deserialize primitive types

问题

我一定做错了... 我试图通过使用ObjecMapper反序列化JSON并分配单个原始类型,但我无法让它工作。

JSON存储在名为“jsonTest.json”的文件中,内容如下:

{
    "simpleVariable": 100
}

然后我有一个用于读取JSON的方法,其中包含以下代码:

ObjectMapper objectMapper = new ObjectMapper();
Path path = Paths.get("jsonTest.json");

// 将JSON读入JSON节点
JsonNode rootNode;
try (Reader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
    // 读取JSON文件
    rootNode = objectMapper.readTree(reader); 
} catch(IOException ie) {
    rootNode = null;
    ie.printStackTrace();
}   

// 现在尝试将节点的值分配给一个int变量:
int intVar = objectMapper.readValue(rootNode, int.class);

问题就在这里。当我尝试编译这段代码时,我会得到以下错误:

类型ObjectMapper中的方法readValue(JsonParser, Class)对于参数(JsonNode, Class)不适用。

所以显然我已经给readValue提供了一个JsonParser对象,但它不能接受,但我又该如何反序列化原始类型呢?

英文:

I must be doing this wrong... I'm trying to assign a single primitive type by deserialising json with ObjecMapper but I can't get it to work.

The json is in a file called "jsonTest.json" which looks like this:

{
    "simpleVariable":100
}

I then have a method for reading json with the following code in it:

ObjectMapper objectMapper = new ObjectMapper();
Path path = Paths.get("jsonTest.json");

// read the json into a json node
JsonNode rootNode;
try (Reader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
    // read the json file
    rootNode = objectMapper.readTree(reader); 
} catch(IOException ie) {
    rootNode = null;
    ie.printStackTrace();
}   

// now attempt to assign the value of the node to an int:
int intVar = objectMapper.readValue(rootNode, int.class);

And herein lies the problem. When I try to compile this code I get the following error:

> The method readValue(JsonParser, Class<T>) in the type ObjectMapper is not applicable for the arguments (JsonNode, Class<Integer>).

So obviously I've fed readValue with a JsonParser object which it can't accept but how else would I deserialise a primitive?

答案1

得分: 0

尝试:

int intVar = rootNode.get("simpleVariable").asInt();
英文:

Try:

int intVar = rootNode.get(&quot;simpleVariable&quot;).asInt();

答案2

得分: 0

你可以创建一个名为JsonTest.java的类

public class JsonTest {

    int simpleVariable; // 与json文件中的名称相同,带有getter和setter方法

    public int getSimpleVariable() {
        return simpleVariable;
    }

    public void setSimpleVariable(int simpleVariable) {
        this.simpleVariable = simpleVariable;
    }

}

然后让Jackson负责解析这些值

示例:

public static void main(String[] args) throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    Path path = Paths.get("jsonTest.json");
    JsonTest jsonTest = objectMapper.readValue(path.toFile(), JsonTest.class);
    int intVar = jsonTest.getSimpleVariable();
    System.out.println("simpleVariable " + intVar);
}
英文:

you can make class named JsonTest.java

public class JsonTest {


    int simpleVariable; // same name as in json file with getters and setters

    public int getSimpleVariable() {
        return simpleVariable;
    }

    public void setSimpleVariable(int simpleVariable) {
        this.simpleVariable = simpleVariable;
    }

}

and let jackson take care of parsing the values

Example:

 public static void main(String[] args) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        Path path = Paths.get(&quot;jsonTest.json&quot;);
        JsonTest jsonTest=objectMapper.readValue(path.toFile(), JsonTest.class);
        int intVar = jsonTest.getSimpleVariable();
        System.out.println(&quot;simpleVariable &quot;+intVar );
}

huangapple
  • 本文由 发表于 2020年7月26日 21:49:17
  • 转载请务必保留本文链接:https://java.coder-hub.com/63101018.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定