杰克逊将json属性反序列化为对象 [英] Jackson deserialize json property into object

查看:93
本文介绍了杰克逊将json属性反序列化为对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要反序列化以下json:

I need to deserialize the following json:

{
    //...
    "foo_id":1
    //...
}

进入对象类Foo的 id 属性设置为 foo_id json属性。

Into an object of class Foo with its id property set to the foo_id json property.

我需要在自定义反序列化器中执行此操作。
完成此任务最简单的方法是什么?

I need to do this within a custom deserializer. What is the most easy way to accomplish this?

我想以某种方式将json转换为

I was thinking to somehow "transform" the json to

{
    //...
    "foo_id":{
        "id":1
    }
    //...
}

然后将其委托给杰克逊。

and then delegate this back to Jackson.

在这种情况下,对象的类型为Foo,但还有其他可能不属于此类的对象。此外,在这种情况下,json是一个数字,但我想支持它是否也是一个字符串。
所以,我需要一种通用的方式来做到这一点,这就是为什么我认为委托给杰克逊可能是一个好主意。

In this case, the object is of type Foo, but there are others which might not be of this class. Also, in this case, that json is a number, but I would like to support if it was a string as well. So, I need a kind of generic way to do this, that's why I think delegating back to Jackson might be a good idea.

不允许注释。假设您已经为此属性编写了反序列化器。

No annotations allowed. Suppose you're already writing the Deserializer for this property.

推荐答案

您是否考虑过使用混合注释?使用Jackson 2.2,您还可以使用转换器进行两步处理( @JsonDeserialize(converter = MyConverter.class )。

Have you considered use of mix-in annotations? With Jackson 2.2, you could also use converters to do two-step processing (@JsonDeserialize(converter=MyConverter.class).

这篇关于杰克逊将json属性反序列化为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆