简体   繁体   中英

Java Jackson: JSON value unknown

I'm still learning how to use Jackson...

So I have a JSON object that has a value that sometimes is an Integer, a long String, or a List

Value: Integer

{
  "id":1,
  "active":1,
  "name":"name1",
  "value":155,
  ...

Value: String

{
  "id":2,
  "active":1,
  "name":"name2",
  "value":"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book...",
  ...

Value: List

{
  "id":3,
  "active":1,
  "name":"name3",
  "value":[
    "One",
    "Two",
    "Three",
    "Four"],
  ...

So all together it looks like...

{
  {
      "id":1,
      "active":1,
      "name":"name1",
      "value":155,
      ...
  },
  {
      "id":2,
      "active":1,
      "name":"name2",
      "value":"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book...",
      ...
  },
  {
      "id":3,
      "active":1,
      "name":"name3",
      "value":[
        "One",
        "Two",
        "Three",
        "Four"],
      ...
  }
}

Here my POJO Model

@JsonIgnoreProperties(ignoreUnknown=true)
@JsonAutoDetect(fieldVisibility= JsonAutoDetect.Visibility.ANY)
public class OQScoresRows {
  private int id;
  private int active;
  private String name;
  private List<String> value;
  ... ...

Here's my mapper code

ObjectMapper mapper = new ObjectMapper();
try{
  POJO obj = mapper.readValue(<JSONOBJECT>, POJO.class);
}catch(JsonParseException e){
  return mapper.writeValueAsString(e);
}

The problem is when I execute my code, I get the following error:

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of VALUE_NUMBER_INT token

It's clear to me that this is happening because "value" can contain one of three different types, how do I make my code flexible enough to accommodate the types...I can always in other methods detect if the value is an int, List, or String but I first need to model (don't I)...

My question is simple: how do I make my code flexible enough to accommodate the types...

If it can be either of Integer , List and String then you can declare it as an Object and cast it later with instanceof , eg:

@JsonIgnoreProperties(ignoreUnknown=true)
@JsonAutoDetect(fieldVisibility= JsonAutoDetect.Visibility.ANY)
public class OQScoresRows {
  private int id;
  private int active;
  private String name;
  private Object value;

After, deserializeing, you can write similar logic to the following:

if(value instanceof Integer){
    //do something after casting it to Integer
}else if(value instanceof List){
    //do something after casting it to List
}else if(value instanceof String){
    do something after casting it to String
}

If-else statement is fine for your question, but it's all string format in json objects, so you have to figure out a way to identity data types from values. For example, Integer.valueOf(value) to identity int; start with [ to identity list; another is string type. You can refer to this answer , which is a general way to convert the json string to an object or a list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM