问题
JSON FORMAT:
[
{
"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: 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"])
My pojo classes are :
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonProperty("0")
private ThirdPartySubContentDetails subContent;
@JsonProperty("video")
private List<ThirdPartySubContentVideoInfo> video;
My Sub class pojo is :
private String src;
@JsonIgnore
@JsonProperty("DRM")
private String drm;
Please help me to write a pojo for that video list.
回答1:
Your json starts as an array and not as an Object. The important part to change is how the Objectmapper should generate your json. For returning a List you need to do it this way:
List<FirstJson> jsonList = mapper.readValue(json, new TypeReference<List<FirstJson>>(){});
Here is my short working test I implement locally:
public static void main(String[] args) {
String json = "[{\"0\":{\"cast\":\"\",\"showname\":\"wohpagle\",\"type\":\"Episodes\"},\"video\":[{\"src\":\"video.mp4\"},{\"DRM\":\"False\"}]}]";
ObjectMapper mapper = new ObjectMapper();
List<FirstJson> jsonList = mapper.readValue(json, new TypeReference<List<FirstJson>>(){});
System.out.println(jsonList.toString());
}
The first part of your JsonArray in Pojo.(Named it FirstJson)
public class FirstJson{
@JsonProperty("0")
private FirstJson subContent;
private String cast;
private String showname;
private String type;
@JsonProperty("video")
private List<Video> videos;
//getter/setter
And the Video Pojo:
public class Video {
private String src;
@JsonProperty("DRM")
private String drm;
//getter/setter
Just a sidenote: If you declare your pojos in the same class file, the classes should be static. public static class FirstJson
回答2:
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>>(){});
来源:https://stackoverflow.com/questions/36786514/map-json-to-pojo-using-jackson-for-list-that-have-different-parameters