使用具有不同参数的Jackson列表将JSON映射到pojo [英] Map JSON to pojo using Jackson for List that have different parameters

查看:52
本文介绍了使用具有不同参数的Jackson列表将JSON映射到pojo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSON格式:

[
{
    "0":
    {
        "cast":"",
        "showname":"woh pagle",
        "type":"Episodes"
    },
    "video":[
        {
            "src":"video.mp4"
        },
        {
            "DRM":"False"
        }
    ]
}
]

这里的问题是我遇到了以下异常:

Here problem is I am getting below exception:

org.codehaus.jackson.map.JsonMappingException:无法反序列化 [来源:START_OBJECT令牌中的java.util.ArrayList实例. java.io.StringReader@1c9ca1;行:1,列:55617](通过 参考链: com.apalya.myplex.valueobject.ThirdPartyContentDetailsArray ["video"])

org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at [Source: java.io.StringReader@1c9ca1; line: 1, column: 55617] (through reference chain: com.apalya.myplex.valueobject.ThirdPartyContentDetailsArray["video"])

我的pojo课是:

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonProperty("0")
private ThirdPartySubContentDetails subContent;

@JsonProperty("video")
private List<ThirdPartySubContentVideoInfo> video;

我的子类pojo是:

private String src;

@JsonIgnore
@JsonProperty("DRM")
private String drm;

请帮助我为该视频列表编写pojo.

Please help me to write a pojo for that video list.

推荐答案

根据问题中描述的JSON结构,以下应该是POJO:

According to the JSON structure described in the question, the following should be the POJOs:

public class MainPojo
{
     @JsonProperty("0")
     private ThirdPartySubContentDetails subContent;

     @JsonProperty("video")
     private List<ThirdPartySubContentVideoInfo> video;

     // Getters and Setters for subContent and video
}

class ThirdPartySubContentDetails
{
    private String cast;
    private String showName;
    private String type;

    // Getters and Setters for cast, showName and type
}

@JsonIgnoreProperties(ignoreUnknown = true)
class ThirdPartySubContentVideoInfo
{
    @JsonProperty("src")
    private String src;

    @JsonProperty("DRM")
    private String drm;

    // Getters and Setters for src and drm
}

您应按以下方式调用解串器方法:

You should call the deserializer method as follows:

 List<MainPojo> list = new ObjectMapper().readValue(json, new TypeReference<List<MainPojo>>(){});

这篇关于使用具有不同参数的Jackson列表将JSON映射到pojo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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