杰克逊映射字符串或简单字符串列表 [英] Jackson Mapping List of String or simple String

查看:200
本文介绍了杰克逊映射字符串或简单字符串列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从API获取一些Json并将它们解析为一些POJO以与它们一起工作但我有这种情况我可以获得一个简单的字符串或字符串数​​组的键。

I'm trying to get some Json from an API and parse them into some POJO's to work with them but i have this case where i can get for a key a simple String or an arrayList of Strings.

Json看起来像这样:

The Json looks like this :

{
  "offerDisplayCategoryMapping": [
    {
      "offerKey": "EUICC_BASE_ACTIVATION_V01",
      "categoriesKeys": {
        "categoryKey": "Included"
      }
    },
    {
      "offerKey": "EUICC_BASE_ACTIVATION_V02",
      "categoriesKeys": {
        "categoryKey": "Included"
      }
    },
    {
      "offerKey": "EUICC_BASE_ACTIVATION_V03",
      "categoriesKeys": {
        "categoryKey": [
          "Option",
          "Included"
        ]
      }
    }]
}

我正在使用Spring Rest从API获得结果。我使用 List< String> 创建了一个代表 categoriesKeys 的POJO,用于定义 categoryKey 并且在我的 RestTemplate 中我定义了一个 ObjectMapper ,其中我启用了 DeserializationFeature。 ACCEPT_SINGLE_VALUE_AS_ARRAY 对于简单字符串的情况但这不起作用!!

I'm using Spring Rest to get the result from the API. I created a POJO that represents categoriesKeys with a List<String> that defines categoryKey and in my RestTemplate I defined an ObjectMapper where i enabled DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY for the case of simple strings but this doesn't work!!

有任何建议吗?

推荐答案

除了已经提到的全局配置外,还可以在个别属性上支持此功能:

In addition to global configuration already mentioned, it is also possible to support this on individual properties:

public class Container {
  @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
  // ... could also add Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED
  public List<String> tags;
}

这篇关于杰克逊映射字符串或简单字符串列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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