我无法将此json转换为java类,我的问题是"值"在这个json文件中,某些位置的值是数组,有些位置是jost键值!! 我使用IntelliJ,这是我的错误:
2017-09-18 14:43:21.540 WARN 5496 --- [apr-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize instance of com.ada.bean.response.Issue out of START_ARRAY token
at [Source:(PushbackInputStream); line:1,column:1];嵌套异常是com.fasterxml.jackson.databind.JsonMappingException:无法从START_ARRAY标记中反序列化com.ada.bean.response.Issue的实例 在[来源:( PushbackInputStream); line:1,column:1]
这是我的json:
[
{
"id": "ETC-1",
"entityId": "94-5024",
"jiraId": null,
"field": [
{
"name": "projectShortName",
"value": "ETC"
},
{
"name": "numberInProject",
"value": "1"
},
{
"name": "summary",
"value": "????? API ??? Youtrack ? ????? ?? ??"
},
{
"name": "created",
"value": "1504079347372"
},
{
"name": "updated",
"value": "1504341622265"
},
{
"name": "updaterName",
"value": "a.fathizade"
},
{
"name": "updaterFullName",
"value": "Ali Fathizadeh"
},
{
"name": "resolved",
"value": "1504341622257"
},
{
"name": "reporterName",
"value": "v.sabeti"
},
{
"name": "reporterFullName",
"value": "???? ?????"
},
{
"name": "commentsCount",
"value": "0"
},
{
"name": "votes",
"value": "0"
},
{
"name": "Priority",
"value": [
"Normal"
],
"valueId": [
"Normal"
],
"color": {
"bg": "#e6f6cf",
"fg": "#4da400"
}
},
{
"name": "Type",
"value": [
"Task"
],
"valueId": [
"Task"
],
"color": null
},
{
"name": "State",
"value": [
"Fixed"
],
"valueId": [
"Fixed"
],
"color": null
},
{
"name": "Assignee",
"value": [
{
"value": "a.fathizade",
"fullName": "Ali Fathizadeh"
}
]
},
{
"name": "Subsystem",
"value": [
"Employees"
],
"valueId": [
"Employees"
],
"color": {
"bg": "#ff7123",
"fg": "#fff"
}
},
{
"name": "sprint",
"value": [
{
"value": "First sprint",
"id": "Etc Board:First sprint"
}
]
}
],
"comment": [],
"tag": []
},
{
"id": "ETC-2",
"entityId": "94-5026",
"jiraId": null,
"field": [
{
"name": "projectShortName",
"value": "ETC"
},
{
"name": "numberInProject",
"value": "2"
},
{
"name": "summary",
"value": "???? ?????? ???? ? ??? ??? ????"
},
{
"name": "created",
"value": "1504079557400"
},
{
"name": "updated",
"value": "1504341630002"
},
{
"name": "updaterName",
"value": "a.fathizade"
},
{
"name": "updaterFullName",
"value": "Ali Fathizadeh"
},
{
"name": "resolved",
"value": "1504341629992"
},
{
"name": "reporterName",
"value": "v.sabeti"
},
{
"name": "reporterFullName",
"value": "???? ?????"
},
{
"name": "commentsCount",
"value": "0"
},
{
"name": "votes",
"value": "0"
},
{
"name": "Priority",
"value": [
"Normal"
],
"valueId": [
"Normal"
],
"color": {
"bg": "#e6f6cf",
"fg": "#4da400"
}
},
{
"name": "Type",
"value": [
"Task"
],
"valueId": [
"Task"
],
"color": null
},
{
"name": "State",
"value": [
"Fixed"
],
"valueId": [
"Fixed"
],
"color": null
},
{
"name": "Assignee",
"value": [
{
"value": "a.fathizade",
"fullName": "Ali Fathizadeh"
}
]
},
{
"name": "Subsystem",
"value": [
"Employees"
],
"valueId": [
"Employees"
],
"color": {
"bg": "#ff7123",
"fg": "#fff"
}
},
{
"name": "sprint",
"value": [
{
"value": "First sprint",
"id": "Etc Board:First sprint"
}
]
}
],
"comment": [],
"tag": []
},
{
"id": "ETC-4",
"entityId": "94-5030",
"jiraId": null,
"field": [
{
"name": "projectShortName",
"value": "ETC"
},
{
"name": "numberInProject",
"value": "4"
},
{
"name": "summary",
"value": "????? ??????? ???? ?? Youtrack ? ????? ?? ?? ?????? ????"
},
{
"name": "created",
"value": "1504079626044"
},
{
"name": "updated",
"value": "1504341630988"
},
{
"name": "updaterName",
"value": "a.fathizade"
},
{
"name": "updaterFullName",
"value": "Ali Fathizadeh"
},
{
"name": "reporterName",
"value": "v.sabeti"
},
{
"name": "reporterFullName",
"value": "???? ?????"
},
{
"name": "commentsCount",
"value": "0"
},
{
"name": "votes",
"value": "0"
},
{
"name": "Priority",
"value": [
"Normal"
],
"valueId": [
"Normal"
],
"color": {
"bg": "#e6f6cf",
"fg": "#4da400"
}
},
{
"name": "Type",
"value": [
"Task"
],
"valueId": [
"Task"
],
"color": null
},
{
"name": "State",
"value": [
"In Progress"
],
"valueId": [
"In Progress"
],
"color": null
},
{
"name": "Assignee",
"value": [
{
"value": "a.fathizade",
"fullName": "Ali Fathizadeh"
}
]
},
{
"name": "Subsystem",
"value": [
"Employees"
],
"valueId": [
"Employees"
],
"color": {
"bg": "#ff7123",
"fg": "#fff"
}
},
{
"name": "sprint",
"value": [
{
"value": "First sprint",
"id": "Etc Board:First sprint"
}
]
}
],
"comment": [],
"tag": []
},
{
"id": "ETC-5",
"entityId": "94-5032",
"jiraId": null,
"field": [
{
"name": "projectShortName",
"value": "ETC"
},
{
"name": "numberInProject",
"value": "5"
},
{
"name": "summary",
"value": "Parse ???? ??????? ???? Excel ????????"
},
{
"name": "created",
"value": "1504079740110"
},
{
"name": "updated",
"value": "1505302061947"
},
{
"name": "updaterName",
"value": "a.fathizade"
},
{
"name": "updaterFullName",
"value": "Ali Fathizadeh"
},
{
"name": "resolved",
"value": "1505302061939"
},
{
"name": "reporterName",
"value": "v.sabeti"
},
{
"name": "reporterFullName",
"value": "???? ?????"
},
{
"name": "commentsCount",
"value": "0"
},
{
"name": "votes",
"value": "0"
},
{
"name": "Priority",
"value": [
"Normal"
],
"valueId": [
"Normal"
],
"color": {
"bg": "#e6f6cf",
"fg": "#4da400"
}
},
{
"name": "Type",
"value": [
"Task"
],
"valueId": [
"Task"
],
"color": null
},
{
"name": "State",
"value": [
"Fixed"
],
"valueId": [
"Fixed"
],
"color": null
},
{
"name": "Assignee",
"value": [
{
"value": "a.fathizade",
"fullName": "Ali Fathizadeh"
}
]
},
{
"name": "Subsystem",
"value": [
"Employees"
],
"valueId": [
"Employees"
],
"color": {
"bg": "#ff7123",
"fg": "#fff"
}
},
{
"name": "sprint",
"value": [
{
"value": "First sprint",
"id": "Etc Board:First sprint"
}
]
}
],
"comment": [],
"tag": []
},
{
"id": "ETC-6",
"entityId": "94-5034",
"jiraId": null,
"field": [
{
"name": "projectShortName",
"value": "ETC"
},
{
"name": "numberInProject",
"value": "6"
},
{
"name": "summary",
"value": "????? ??????? Youtrack ????? ?? DB ?? ??????? ???"
},
{
"name": "description",
"value": "????? ??????? ??? ??? ?? ???? ?????"
},
{
"name": "created",
"value": "1504079823857"
},
{
"name": "updated",
"value": "1504079836355"
},
{
"name": "updaterName",
"value": "v.sabeti"
},
{
"name": "updaterFullName",
"value": "???? ?????"
},
{
"name": "reporterName",
"value": "v.sabeti"
},
{
"name": "reporterFullName",
"value": "???? ?????"
},
{
"name": "commentsCount",
"value": "0"
},
{
"name": "votes",
"value": "0"
},
{
"name": "Priority",
"value": [
"Normal"
],
"valueId": [
"Normal"
],
"color": {
"bg": "#e6f6cf",
"fg": "#4da400"
}
},
{
"name": "Type",
"value": [
"Task"
],
"valueId": [
"Task"
],
"color": null
},
{
"name": "State",
"value": [
"Open"
],
"valueId": [
"Open"
],
"color": null
},
{
"name": "Assignee",
"value": [
{
"value": "a.fathizade",
"fullName": "Ali Fathizadeh"
}
]
},
{
"name": "Subsystem",
"value": [
"Employees"
],
"valueId": [
"Employees"
],
"color": {
"bg": "#ff7123",
"fg": "#fff"
}
},
{
"name": "sprint",
"value": [
{
"value": "First sprint",
"id": "Etc Board:First sprint"
}
]
}
],
"comment": [],
"tag": []
},
{
"id": "ETC-7",
"entityId": "94-5036",
"jiraId": null,
"field": [
{
"name": "projectShortName",
"value": "ETC"
},
{
"name": "numberInProject",
"value": "7"
},
{
"name": "summary",
"value": "????????? ?? ????? ?????? ? ????? ?? ?? ??? ??????"
},
{
"name": "created",
"value": "1504079868785"
},
{
"name": "updated",
"value": "1504079868785"
},
{
"name": "updaterName",
"value": "v.sabeti"
},
{
"name": "updaterFullName",
"value": "???? ?????"
},
{
"name": "reporterName",
"value": "v.sabeti"
},
{
"name": "reporterFullName",
"value": "???? ?????"
},
{
"name": "commentsCount",
"value": "0"
},
{
"name": "votes",
"value": "0"
},
{
"name": "Priority",
"value": [
"Normal"
],
"valueId": [
"Normal"
],
"color": {
"bg": "#e6f6cf",
"fg": "#4da400"
}
},
{
"name": "Type",
"value": [
"Task"
],
"valueId": [
"Task"
],
"color": null
},
{
"name": "State",
"value": [
"Open"
],
"valueId": [
"Open"
],
"color": null
},
{
"name": "Assignee",
"value": [
{
"value": "a.fathizade",
"fullName": "Ali Fathizadeh"
}
]
},
{
"name": "Subsystem",
"value": [
"Employees"
],
"valueId": [
"Employees"
],
"color": {
"bg": "#ff7123",
"fg": "#fff"
}
},
{
"name": "sprint",
"value": [
{
"value": "First sprint",
"id": "Etc Board:First sprint"
}
]
}
],
"comment": [],
"tag": []
}
]
这是我为这个json写的类: 的问题:
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Issues implements Serializable
{
@JsonProperty
private Issue[] issue;
public Issue[] getIssue ()
{
return issue;
}
public void setIssue (Issue[] issue)
{
this.issue = issue;
}
@Override
public String toString()
{
return "ClassPojo [issue = "+issue+"]";
}
}
问题:
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Issue implements Serializable
{
@JsonProperty
private String id;
@JsonProperty
private Field [] field;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Field[] getField() {
return field;
}
public void setField(Field[] field) {
this.field = field;
}
@Override
public String toString() {
return "Issue{" +
"id='" + id + '\'' +
", field=" + Arrays.toString(field) +
'}';
}
}
字段:
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Field implements Serializable {
@JsonProperty
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Field{" +
"name='" + name + '\'' +
// ", value='" + value + '\'' +
'}';
}
}
答案 0 :(得分:0)
因为你的" root"实体是一种数组(如异常消息所示),(据我所知),Jackson Mapper使用从Collection继承的方法。 因此,最快速和最简单的方法是从Collection实现继承方法,例如ArrayList的:
...
public class Issues extends ArrayList<Issue> {
@Override
public String toString() {
return "ClassPojo [issue = " + super.toString() + "]";
}
}
请注意,您不需要&#34;问题&#34;属性不再,数组由List实现在内部存储。 上面的示例适用于我,从com.fasterxml.jackson.databind.ObjectMapper调用(其中getJson()完全返回您提供的String):
ObjectMapper mapperObj = new ObjectMapper();
try {
Issues li = mapperObj.readValue(getJson(),Issues.class);
System.out.println(li);
} catch (IOException e) {
e.printStackTrace();
}
输出:
ClassPojo [issue = [Issue{id='ETC-1', field=[Field{name='projectShortName'}, Field{name='numberInProject'}, Field{name='summary'}, Field{name='created'}, Field{name='updated'}, Field{name='updaterName'}, Field{name='updaterFullName'}, Field{name='resolved'}, Field{name='reporterName'}, Field{name='reporterFullName'}, Field{name='commentsCount'}, Field{name='votes'}, Field{name='Priority'}, Field{name='Type'}, Field{name='State'}, Field{name='Assignee'}, Field{name='Subsystem'}, Field{name='sprint'}]}, Issue{id='ETC-2', field=[Field{name='projectShortName'}, Field{name='numberInProject'}, Field{name='summary'}, Field{name='created'}, Field{name='updated'}, Field{name='updaterName'}, Field{name='updaterFullName'}, Field{name='resolved'}, Field{name='reporterName'}, Field{name='reporterFullName'}, Field{name='commentsCount'}, Field{name='votes'}, Field{name='Priority'}, Field{name='Type'}, Field{name='State'}, Field{name='Assignee'}, Field{name='Subsystem'}, Field{name='sprint'}]}, Issue{id='ETC-4', field=[Field{name='projectShortName'}, Field{name='numberInProject'}, Field{name='summary'}, Field{name='created'}, Field{name='updated'}, Field{name='updaterName'}, Field{name='updaterFullName'}, Field{name='reporterName'}, Field{name='reporterFullName'}, Field{name='commentsCount'}, Field{name='votes'}, Field{name='Priority'}, Field{name='Type'}, Field{name='State'}, Field{name='Assignee'}, Field{name='Subsystem'}, Field{name='sprint'}]}, Issue{id='ETC-5', field=[Field{name='projectShortName'}, Field{name='numberInProject'}, Field{name='summary'}, Field{name='created'}, Field{name='updated'}, Field{name='updaterName'}, Field{name='updaterFullName'}, Field{name='resolved'}, Field{name='reporterName'}, Field{name='reporterFullName'}, Field{name='commentsCount'}, Field{name='votes'}, Field{name='Priority'}, Field{name='Type'}, Field{name='State'}, Field{name='Assignee'}, Field{name='Subsystem'}, Field{name='sprint'}]}, Issue{id='ETC-6', field=[Field{name='projectShortName'}, Field{name='numberInProject'}, Field{name='summary'}, Field{name='description'}, Field{name='created'}, Field{name='updated'}, Field{name='updaterName'}, Field{name='updaterFullName'}, Field{name='reporterName'}, Field{name='reporterFullName'}, Field{name='commentsCount'}, Field{name='votes'}, Field{name='Priority'}, Field{name='Type'}, Field{name='State'}, Field{name='Assignee'}, Field{name='Subsystem'}, Field{name='sprint'}]}, Issue{id='ETC-7', field=[Field{name='projectShortName'}, Field{name='numberInProject'}, Field{name='summary'}, Field{name='created'}, Field{name='updated'}, Field{name='updaterName'}, Field{name='updaterFullName'}, Field{name='reporterName'}, Field{name='reporterFullName'}, Field{name='commentsCount'}, Field{name='votes'}, Field{name='Priority'}, Field{name='Type'}, Field{name='State'}, Field{name='Assignee'}, Field{name='Subsystem'}, Field{name='sprint'}]}]]