当JsonProperty有时是数组,有时是单个对象时,Jackson会对其进行解析 [英] Jackson desrialize when JsonProperty is sometimes array and sometimes a single Object

查看:206
本文介绍了当JsonProperty有时是数组,有时是单个对象时,Jackson会对其进行解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在发布之前搜索过Stack Overflow,但是Jackson没有解决方案。


这是服务器响应:

I've searched Stack Overflow before posting, but there were no solutions for Jackson.

Here is a server response:

{
  "ok": true,
  "result": [
    {
      "update_id": 489881731,
      //rest 
    },
    {
      "update_id": 489881732,
      //rest
    }
  ]
}

如您所见,物业结果是一个数组。


现在这是另一个回复:

As you see property "result" is an array.

Now this is another response:

{
  "ok": true,
  "result": {
    "id": 211948704,
    "first_name": "ربات ادمین‌های تلگرام",
    "username": "tgAdminsBot"
  }
}

此处result是一个单独的对象。


这是我的类,我想将内容反序列化。我为 TObject 写了一个自定义反序列化器当然:

Here "result" is a single object.

This is my class I want to deserialize content to it. I wrote a custom deserializer for TObject of course:

public class Result
{
    private TObject[] result;
    private boolean ok;

    public void setOk (boolean ok) {//code}

    public void setResult (TObject[] result) {//code}


    public TObject[] getResult () {//code}

    public boolean getOk (){//code}
}

所以我在课堂上假设 result是一个 TObject的秒。现在我该怎么办? 对两个字段使用 @JsonProperty(result),其中一个字段是 TObject s和一个是单个 TObject 好吗?


如果不是我还能做什么?

So I assumed in my class that "result" is an array of TObjects. Now what can I do? Is using @JsonProperty("result") for two fields which one is an array of TObjects and one is a single TObject OK?

If not what else can I do?

推荐答案

如果启用 DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY 两者

{result:[]}

{result: {}} 可以解析为

class Result{
   List result;
}

文档链接


确定是否可以强制使用非数组
(使用JSON)值来使用Java集合(数组,
java.util.Collection)类型的功能。如果启用,收集反序列化器将
尝试处理非数组值,就像它们围绕
JSON数组隐式一样。此功能旨在用于
兼容性/互操作性原因,用于处理包(如
XML-to-JSON转换器)在没有
的情况下遗漏JSON数组的包数组中的单个元素。默认情况下禁用功能。

Feature that determines whether it is acceptable to coerce non-array (in JSON) values to work with Java collection (arrays, java.util.Collection) types. If enabled, collection deserializers will try to handle non-array values as if they had "implicit" surrounding JSON array. This feature is meant to be used for compatibility/interoperability reasons, to work with packages (such as XML-to-JSON converters) that leave out JSON array in cases where there is just a single element in array. Feature is disabled by default.

演示如何将其用于OP输入jsons和POJO:

Demo how to use it for OP input jsons and POJOs:

ObjectMapper mapper = new ObjectMapper()
mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
Result result = mapper.readValue(Result.class;

可与<$一起使用c $ c> @JsonFormat 如果由于某种原因你不喜欢mapper版本

Can be used with @JsonFormat above class or field if you don't like mapper version for some reason

@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)

PS。其他有关此问题的SO问题: LINK

PS. Other SO questions about this problem: LINK

这篇关于当JsonProperty有时是数组,有时是单个对象时,Jackson会对其进行解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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