简体   繁体   English

Jackson-忽略数据类型不匹配的属性

[英]Jackson - Ignore properties whose data type does not match

I have many JSON files, which i am parsing using Jackson. 我有许多JSON文件,我正在使用Jackson进行解析。 So a part of my JSON looks like 所以我的JSON的一部分看起来像

"data": {
    "k": "ewt",
    "e": "dwpc",
    "d": 2,
    "ex": 0,
    "t": 3439
}

"data": {
    "k": "mmm1",
    "e": [{
        "x": 548,
        "y": 330,
        "t": 35733
    }, {
        "x": 541,
        "y": 342,
        "t": 36354
    }],
    "min": 0,
    "max": 0,
    "avg": 0
}

Here, if iyou notice, in the first "data" block type of "e" is string and in the second the type is Array. 在这里,如果iyou注意到,在第一个“数据”块中,类型“ e”是字符串,在第二个中,类型是Array。 My concern is only with the second type of "e" that is array, so I made my POJO beans as follows - 我只关心数组的第二种类型“ e”,因此我按如下方式制作了POJO bean:

Class data.java

...
@JsonProperty("e")
private List<MouseDataArray> e = new ArrayList<MouseDataArray>();

some more properties .. and getters and setters .. 

And

Class MouseDataArray.java

@JsonInclude(JsonInclude.Include.NON_NULL)


@JsonPropertyOrder({
    "x",
    "y",
    "t"
})
public class MouseDataArray {

    @JsonProperty("x")
    private Long x;
    @JsonProperty("y")
    private Long y;
    @JsonProperty("t")
    private Long t;

.. getters and setters
}

Now the thing is I am only concerned with the property "e" of type Array, and not interested in the String type property "e". 现在的问题是,我只关心数组类型的属性“ e”,而不关心字符串类型的属性“ e”。 So When it parses, it throuws exception Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token Which is expected, but I want it to just skip the property e of type string rather than trying to map it to the Array type. 因此,在解析时,它会引发异常。 无法从VALUE_STRING令牌中反序列化java.util.ArrayList的实例 ,但是我希望它仅跳过字符串类型的属性e而不是尝试将其映射为Array类型。

Thanks. 谢谢。

It seems that incoming JSON is badly designed, since it uses both regular String type and Object for 'e' -- most OO languages do not have real common base type for such combination. 传入的JSON似乎设计不好,因为它同时使用常规String类型和Object作为'e'-大多数OO语言对于这种组合都没有真正的通用基类型。 So your only bet for data-binding is to declare e to be either java.lang.Object (in which case it'll become either java.lang.String or java.util.Map ), or JsonNode ("JSON Tree"). 因此,唯一的数据绑定选择是将e声明为java.lang.Object (在这种情况下,它将成为java.lang.Stringjava.util.Map )或JsonNode (“ JSON树”) 。 And then you will need to extract data out after binding. 然后,您需要在绑定后提取数据。

Although you could implement a custom deserializer, it is often simpler to just use Object or JsonNode as initial type, and then handle later conversion. 尽管可以实现自定义反序列化器,但通常更简单的方法是只使用ObjectJsonNode作为初始类型,然后再处理以后的转换。 For example, you can convert from JsonNode into any other type simply with: 例如,您可以使用以下方法将JsonNode转换为任何其他类型:

MyValue v = mapper.treeToValue(treeNode, MyValue.class)

or, if you prefer, just get stuff out if 或者,如果您愿意,只要

JsonNode first = treeNode.get(0);
MyValue v = new MyValue(first.get("x").asInt(), ...);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 杰克逊:如何忽略字段的数据类型不匹配 - Jackson: How to ignore data type mismatch for a field 杰克逊反序列化忽略属性不适用于@JsonView - Jackson deserialization ignore properties does not work properly with @JsonView 如果数据和 model 字段不是同一类型,则忽略 jackson 反序列化 - Ignore jackson deserialization if the data and model field are not of the same type 杰克逊无法识别物业 - Jackson does not recognize properties 用Jackson进行反序列化(对未知属性失败)不会忽略鉴别属性(使用SwaggerCodegen创建的DTO) - Deserializing with Jackson (fail on unknown properties) does not ignore discriminator property (DTO's created with SwaggerCodegen) 杰克逊使用相同的类忽略属性 - Jackson ignore properties using the same class 如何使用Jackson AnnotationIntrospector有条件地忽略属性 - How to conditionally ignore properties with a Jackson AnnotationIntrospector Jackson ObjectMapper 忽略所有没有注释的属性 - Jackson ObjectMapper ignore all properties that has no annotation Jackson 2.0忽略了课堂上的所有属性 - Jackson 2.0 ignore all properties on a class Jackson 序列化:如何忽略超类属性 - Jackson serialization: how to ignore superclass properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM