user17485523
user17485523

Reputation:

how to serialize this json with jackson?

I have incoming json which looks like this:

{
  "ref": {
    "id": "1011"
  },
  "compset": [
      {
        "id": "23412"
      },
      {
        "id": "27964"
      },
      {
        "id": "51193"
      },
      {
        "id": "74343"
      },
      {
        "id": "537157"
      },
      {
        "id": "542023"
      },
      {
        "id": "601732"
      },
      {
        "id": "793808"
      },
      {
        "id": "891169"
      },
      {
        "id": "1246443"
      }
 ],
   "isCompliant": false,
    "updateTimestamp": null,
    "remainingMilliseconds": 0,
    "totalLockoutDays": 15
}

I have three classes that handle this response:

for "id" field:

public class Competitor {
  @JsonProperty("id")
  @JsonFormat(shape = JsonFormat.Shape.NUMBER_INT)
  private Integer id; // this should be integer, can't change the type
}

for "compset" field:

public class PropertyCompetitorsModel {
  private List<Competitor> competitors;
}

for response itself:

public class CompSetResponse {
  @JsonProperty("compset")
  private PropertyCompetitorsModel compset;
}

As you can see I need only compset field.

With code above I am having this error:

Cannot deserialize instance of PropertyCompetitorsModel out of START_ARRAY token

Jackson library is used

Upvotes: 0

Views: 72

Answers (1)

Jo&#227;o Dias
Jo&#227;o Dias

Reputation: 17460

Use the following instead:

public class CompSetResponse {
  @JsonProperty("compset")
  private List<Competitor> compset;
}

Your current code is expecting a JSON as follows:

{
  "ref": {
    "id": "1011"
  },
  "compset": {
    "competitors": [
        {
          "id": "23412"
        },
        {
          "id": "27964"
        },
        {
          "id": "51193"
        },
        {
          "id": "74343"
        },
        {
          "id": "537157"
        },
        {
          "id": "542023"
        },
        {
          "id": "601732"
        },
        {
          "id": "793808"
        },
        {
          "id": "891169"
        },
        {
          "id": "1246443"
        }
    ],
  },
   "isCompliant": false,
    "updateTimestamp": null,
    "remainingMilliseconds": 0,
    "totalLockoutDays": 15
}

Upvotes: 1

Related Questions