如何用杰克逊序列化这个json?
how to serialize this json with jackson?
我收到了 json,看起来像这样:
{
"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
}
我有三个 类 处理此响应:
对于“id”字段:
public class Competitor {
@JsonProperty("id")
@JsonFormat(shape = JsonFormat.Shape.NUMBER_INT)
private Integer id; // this should be integer, can't change the type
}
对于“compset”字段:
public class PropertyCompetitorsModel {
private List<Competitor> competitors;
}
对于响应本身:
public class CompSetResponse {
@JsonProperty("compset")
private PropertyCompetitorsModel compset;
}
如您所见,我只需要 compset 字段。
使用上面的代码我遇到了这个错误:
Cannot deserialize instance of PropertyCompetitorsModel out of START_ARRAY token
使用了 Jackson 库
改为使用以下内容:
public class CompSetResponse {
@JsonProperty("compset")
private List<Competitor> compset;
}
您当前的代码需要 JSON 如下:
{
"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
}
我收到了 json,看起来像这样:
{
"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
}
我有三个 类 处理此响应:
对于“id”字段:
public class Competitor {
@JsonProperty("id")
@JsonFormat(shape = JsonFormat.Shape.NUMBER_INT)
private Integer id; // this should be integer, can't change the type
}
对于“compset”字段:
public class PropertyCompetitorsModel {
private List<Competitor> competitors;
}
对于响应本身:
public class CompSetResponse {
@JsonProperty("compset")
private PropertyCompetitorsModel compset;
}
如您所见,我只需要 compset 字段。
使用上面的代码我遇到了这个错误:
Cannot deserialize instance of PropertyCompetitorsModel out of START_ARRAY token
使用了 Jackson 库
改为使用以下内容:
public class CompSetResponse {
@JsonProperty("compset")
private List<Competitor> compset;
}
您当前的代码需要 JSON 如下:
{
"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
}