0

Im using jacson to parse the following JSON array

[
  {
"target": "something",
"datapoints": [
  [
  null,
  1482223380
  ]]}]

Into this POJO

public class Response {

  private String target;
  private List<List<Double>> datapoints;

  public String getTarget() {
    return target;
  }

  public void setTarget(String target) {
    this.target = target;
  }

  public List<List<Double>> getData() {
    return datapoints;
  }

  public void setData(List<List<Double>> data) {
    this.datapoints = data;
  }
}

Using the following code

objectMapper.readValue(json, new TypeReference<List<Response>>() {});

This works partially, the outer list and the target is correct, however datapoints is null.

My initial solution is taken from this answere.

My question is, why are not datapoints not parsed as expected? Do this has something todo with the null values inside the array?

1 Answer 1

1

You could write a custom JsonDeserializer for the datapoints field.

class MyDatapointsDeserializer extends JsonDeserializer<List<List<Double>>> {

    private static final TypeReference<List<List<Double>>> TYPE_REF = 
            new TypeReference<List<List<Double>>>() {};

    @Override
    public List<List<Double>> deserialize(
            JsonParser jp, DeserializationContext ctxt) throws IOException {
        return jp.readValueAs(TYPE_REF);
    }
}

Then annotate the field accordingly.

@JsonDeserialize(using = MyDatapointsDeserializer.class)
private List<List<Double>> datapoints;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.