使用 Jackson 从 JSON API 响应生成不同的 Java 对象

发布于 2025-01-14 07:37:14 字数 847 浏览 2 评论 0原文

我目前正在开发一个项目,该项目要求我接受各种货币对并从 API 调用响应生成响应对象。我使用 jackson 将 JSON 响应映射到 java 对象,然后从生成的 ArrayList 读取数据。问题是 JSON 响应可以将不同的货币配对字符串作为配对数据列表的键。典型的响应如下所示:

{"error":[],"result":{"XXBTZUSD":[[1647062100,"39091.2","39184.9","39088.9","39139.0","39150.9","59.22447291",161],}[1647063000,"39138.9","39188.4","39138.9","39151.2","39174.2","2.92905848",126]]}

当我尝试从不同的货币对中提取数据时,就会出现问题,因为我的结果对象被硬编码为提取 JSON 键 XXBTZUSD 的数据。这是我的结果对象的样子:

    public class Result{
    
        @JsonProperty("XXBTZUSD")
        public ArrayList<ArrayList<Object>> candles;
        public int last;
    }

我正在考虑让 @JsonProperty 成为变量并从 json 响应中传递密钥以正确拉出我设置的货币对,但 JsonProperty 需要是一个常量。我能想到的解决这个问题的唯一方法是为每个货币对设置大量不同的类,但这效率很低,并且需要大约 15 个单独的类来完成。我对杰克逊图书馆不太熟悉。如果有人对如何解决这个问题有任何想法,我将非常感激,我已经尝试找到解决这个问题的方法有一段时间了。谢谢你!

I'm working on a project at the moment that requires me to take in a various currency pairs and generate a response object from an API call response. I'm using jackson to map the JSON response to java objects then reading data from an ArrayList generated. The problem is the JSON response can have the different currency pairing strings as a key for the list of pairing data. Here's what a typical response looks like:

{"error":[],"result":{"XXBTZUSD":[[1647062100,"39091.2","39184.9","39088.9","39139.0","39150.9","59.22447291",161],}[1647063000,"39138.9","39188.4","39138.9","39151.2","39174.2","2.92905848",126]]}

The problem arises when I try to pull data from a different currency pair as my result object is hard coded to pull the data for the JSON key XXBTZUSD. Here's what my result object looks like:

    public class Result{
    
        @JsonProperty("XXBTZUSD")
        public ArrayList<ArrayList<Object>> candles;
        public int last;
    }

I was thinking having the @JsonProperty be variable and pass in the key from the json response to correct pull the currnecy pair I set it to, but JsonProperty needs to be a constant. The only way around this I can see is to have a ton of different classes for each currency pair but that would be inefficient and take around 15 separate classes to do. I'm not too familiar with the jackson library. If anyone has any ideas of how to solve this I would be greatly appreciative, I've been trying to figure out a way around this for awhile now. Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

┊风居住的梦幻卍 2025-01-21 07:37:14

如果键可以不同,一种选择是使用 Map。不需要您的 Result 类,将结果属性解析为 Map。然后像这样提取:

Map<String, Object> result = deserialize();
ArrayList<ArrayList<Object>> arrays = (ArrayList<ArrayList<Object>>) result.get("XXBTZUSD");

只需根据您需要的货币对更改属性即可。

另一种选择是编写自定义反序列化器,无论该属性在 json 中如何命名,始终将值放入 candles 字段中。

If keys can be different, one option is to use a Map. Your Result class won't be needed, parse the result property into Map<String, Object>. Then extract like this:

Map<String, Object> result = deserialize();
ArrayList<ArrayList<Object>> arrays = (ArrayList<ArrayList<Object>>) result.get("XXBTZUSD");

Just change property depending on which currency pair you need.

Another option is writing custom deserializer, in which to always put value in your candles field, regardless of how the property is named in json.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文