Jackson根据字符串值将JSON反序列化为不同类型的列表

时间:2017-09-25 15:50:05

标签: java json jackson

我需要使用Jackson将JSON字符串反序列化为不同对象类型的列表,具体取决于其中一个字段中设置的值。我想知道创建不同列表类型的最佳方法是什么以及我如何实现它?

我的JSON看起来像这样:

{"test":
    {"conditions":[....],                   
     "consequence": {"actionType":"string", 
                     "action": ["value 1","value 2"]}                                               
    }       
}

因此,在解析时,上面会返回List<String>

{"test":
    {"conditions":[....],                   
     "consequence": {"actionType":"test", 
                     "action": ["test","test"]}                                             
    }       
}

以上将返回List<Test>

我的Pojo只包含:

  @Data
public class Consequence {

    public Consequence(String actionType){
        this.actionType = actionType;       
    };

    @JsonProperty("ACTIONTYPE")
    private String actionType;

    @JsonProperty("ACTION")
    private List<????> action;
}

更新:

我使用以下层次结构更新了我的POJO后:

@Data
public abstract class BaseConsequence {

    public BaseConsequence(String actionType){
        this.actionType = actionType;       
    };

    @JsonProperty("ACTIONTYPE")
    private String actionType;

}

 @Data
@DiscriminatorValue(value = "CONCATENATE")
public class ConcatenateConsequence extends BaseConsequence {

    public ConcatenateConsequence(String actionType, List<String> concatenateValues) {
        super(actionType);
        this.concatenateValues = concatenateValues;
    }
    private List<String> concatenateValues;
}

@Data
@DiscriminatorValue(value = "test")
public class TestConsequence extends BaseConsequence {

    public TestConsequence(String actionType, List<Test> tests){
        super(actionType);
        this.tests = tests;
    }
    private List<Test> tests;
}

@Data
public class Test {

    public Test(){};

    public Test(List<Condition> conditions, BaseConsequence baseConsequence){
        this.conditions = conditions;
        this.baseConsequence = baseConsequence;     
    }

    @JsonProperty("CONDITIONS")
    private List<Condition> conditions;

    @JsonProperty("CONSEQUENCE")
    private BaseConsequence baseConsequence;

    @Override
    public boolean equals(Object o) {

        if (o == this) return true;

        if (!(o instanceof Test)) {
            return false;
        }        
        Test test = (Test) o;
        return Objects.equals(conditions, test.conditions) && Objects.equals(baseConsequence, test.baseConsequence);
    }

    @Override
    public int hashCode() {
        return Objects.hash(conditions, baseConsequence);
    }   
}

我收到以下错误:

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
 at [Source: {"TEST":{"CONDITIONS":[{"KEY":"KEY1","VALUES":["FLOAT"],"OPERATOR":""}],"CONSEQUENCE":{"ACTIONTYPE" :{"CONCATENATE": ["VALUE1","VALUE2"]}}}}; line: 1, column: 9] (through reference chain: package.TestCase["TEST"])

1 个答案:

答案 0 :(得分:1)

您可以使用两种变体:

  1. 创建自定义反序列化程序。请参阅此处以获取完整说明和示例http://www.baeldung.com/jackson-deserialization
  2. 最好的方法是使用一个基类和两个孩子。每个孩子都应标有@DiscriminatorValue。请参阅此处以获取完整说明和示例http://www.baeldung.com/jackson-inheritance