改变领域的Java杰克逊注释 [英] Java Jackson annotation for changing field

查看:86
本文介绍了改变领域的Java杰克逊注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须为货币汇率调用第三方API,但如果我要求 MYR USD 转换,它会返回我: {MYR_USD:0.246731} ,所以如果我要求 KRW USD ,它会返回 {KRW_USD:0.000888}

I have to call 3rd party API for currency exchange rate, but the JSON returned keep changing, if i request a MYR to USD conversion, it will return me: {"MYR_USD":0.246731}, so if i request for KRW to USD, it will return me {"KRW_USD":0.000888}.

我必须使用 @JsonProperty 将返回的结果映射到pojo,有什么方法可行吗?

I will have to map the returned result into pojo using @JsonProperty, is there any ways that works?

我当前的硬编码解决方法:

My current hard-coded workaround:

@JsonIgnoreProperties(ignoreUnknown = true)
public class FreeCurrencyExchangeRate {

    @JsonProperty("MYR_USD")
    private double rate;

    public double getRate() {
        return rate;
    }

    public void setRate(double rate) {
        this.rate = rate;
    }
} 


推荐答案

{A:5} 是一个字段 A 的对象,其值 5

{A:5} 地图键/值对 A = 5

{"A":5} is an object with field A having value 5.
No {"A":5} is a Map with key/value pair A=5.

两者都可以为真,但由于<$ c $之前的值c>:是动态的(更改),它是您需要的第二种解释。

Both can be true, but since the value before : is dynamic (changing), it the second interpretation you need.

所以不要求将JSON转换为POJO( FreeCurrencyExchangeRate )。

要求将其转换为 Map ,然后迭代那张地图。

So don't ask for JSON to be converted to a POJO (FreeCurrencyExchangeRate).
Ask for it to be converted to a Map, then iterate that map.

这篇关于改变领域的Java杰克逊注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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