当前位置: 首页 > 知识库问答 >
问题:

Jackson JsonMappingException。没有要从字符串值反序列化的字符串参数构造函数/工厂方法

单于俊智
2023-03-14

我正在尝试反序列化以下JSON:

 {
    "name": "myName",
    "decoder": "myDecoder",
    "id": 123,
    "definition": {
        "AND": [
            "and-condition-1",
            "and-condition-2",
            {
                "OR": [
                    "or-condition-1",
                    "or-condition-2"
                ]
            }
        ]
    }
}

我已反序列化文件:

  ObjectMapper mapper = new ObjectMapper();
  RuleDefinition ruleDefinition = mapper.readValue(new File(fileName), RuleDefinition.class);

在以下对象结构中:

规则efinition.java

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class RuleDefinition {

    @JsonProperty("name")
    private String name;

    @JsonProperty("decoder")
    private String decoder;

    @JsonProperty("id")
    private int id;

    @JsonProperty("definition")
    private Definition definition;

    @JsonProperty("name")
    public String getName() {
        return name;
    }

    @JsonProperty("name")
    public void setName(String name) {
        this.name = name;
    }

    @JsonProperty("decoder")
    public String getDecoder() {
        return decoder;
    }

    @JsonProperty("decoder")
    public void setDecoder(String decoder) {
        this.decoder = decoder;
    }

    @JsonProperty("id")
    public int getId() {
        return id;
    }

    @JsonProperty("id")
    public void setId(int id) {
        this.id = id;
    }

    @JsonProperty("definition")
    public Definition getDefinition() {
        return definition;
    }

    @JsonProperty("definition")
    public void setDefinition(Definition definition) {
        this.definition = definition;
    }

    @Override
    public String toString() {
        return "RuleDefinition{" +
                "name='" + name + '\'' +
                ", decoder='" + decoder + '\'' +
                ", id=" + id +
                ", definition=" + definition +
                '}';
    }
}

定义。爪哇

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class Definition {

    @JsonProperty("AND")
    private List<AND> AND;

    public List<AND> getAND() {
        return AND;
    }

    public void setAND(List<AND> AND) {
        this.AND = AND;
    }

    @Override
    public String toString() {
        return "Definition{" +
                "AND=" + AND +
                '}';
    }
}

和JAVA

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class AND {

    @JsonProperty("OR")
    private List<String> Or;

    @JsonProperty("OR")
    public List<String> getOR() {
        return Or;
    }

    @JsonProperty("OR")
    public void setOR(List<String> Or) {
        this.Or = Or;
    }


    @Override
    public String toString() {
        return "AND{" +
                "Or=" + Or +
                '}';
    }
}

我得到了以下错误

com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.model.AND: no String-argument constructor/factory method to deserialize from String value ('and-condition-1')

我看到问题是因为'and-condition-1'只是一个值,我如何反序列化这些,我想知道我应该写一个自定义反序列化器(或者)有什么解决方法吗?

共有1个答案

羊刚捷
2023-03-14

我认为问题在于,在JSON中,AND对象既是字符串又是对象。您应该为它创建一个JsonDeserializer

在此输入链接描述在此输入链接描述

 类似资料: