使用 Jackson 在 Java 中反序列化 JSON
Deserializing JSON in Java using Jackson
我有以下 JSON 的示例片段,我正在尝试反序列化。
{
"total": 2236,
"issues": [{
"id": "10142",
"key": "ID-2",
"fields": {
"attachment": [{
"id": "11132"
}]
}
}]
}
我可以反序列化 id 和 key 的数据,但不能反序列化字段中的附件。我的附件 class 始终为空
这是我的代码。
Response.java
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Response {
@JsonProperty
private int total;
@JsonProperty
private List<Issue> issues;
}
Issue.java
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Issue {
@JsonProperty
private int id;
@JsonProperty
private String key;
@JsonProperty
private Fields fields;
}
Fields.java
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Fields {
@JsonProperty
private Attachments attachment;
}
Attachments.java
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Attachments {
@JsonProperty
private List<Attachment> attachment;
}
Attachments.java
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Attachment {
@JsonProperty
private String id;
}
在你的JSON中,attachment
是一个数组,不是一个对象。
您不需要 Attachments
class,只需这样修改 Fields
:
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Fields {
@JsonProperty
private List<Attachment> attachment;
}
将 Java class 中的每个变量视为对应于 JSON 中的一个属性。 “附件”不在 JSON 文件中。您应该能够删除它并更改 Fields
class.
中的变量定义
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Fields {
@JsonProperty
private List<Attachment> attachment;
}
如果您不想更改 class 结构,则可以修改 JSON。
在attachment
中,您需要再次添加一个attachment
。
JSON 会像下面这样。
{
"total":2233,
"issues":[
{
"id":"13598",
"key":"ID-2368",
"fields":{
"attachment":{
"**attachment**":[
{
"id":"11122"
}
]
}
}
}
]
}
我有以下 JSON 的示例片段,我正在尝试反序列化。
{
"total": 2236,
"issues": [{
"id": "10142",
"key": "ID-2",
"fields": {
"attachment": [{
"id": "11132"
}]
}
}]
}
我可以反序列化 id 和 key 的数据,但不能反序列化字段中的附件。我的附件 class 始终为空
这是我的代码。
Response.java
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Response {
@JsonProperty
private int total;
@JsonProperty
private List<Issue> issues;
}
Issue.java
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Issue {
@JsonProperty
private int id;
@JsonProperty
private String key;
@JsonProperty
private Fields fields;
}
Fields.java
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Fields {
@JsonProperty
private Attachments attachment;
}
Attachments.java
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Attachments {
@JsonProperty
private List<Attachment> attachment;
}
Attachments.java
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Attachment {
@JsonProperty
private String id;
}
在你的JSON中,attachment
是一个数组,不是一个对象。
您不需要 Attachments
class,只需这样修改 Fields
:
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Fields {
@JsonProperty
private List<Attachment> attachment;
}
将 Java class 中的每个变量视为对应于 JSON 中的一个属性。 “附件”不在 JSON 文件中。您应该能够删除它并更改 Fields
class.
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Fields {
@JsonProperty
private List<Attachment> attachment;
}
如果您不想更改 class 结构,则可以修改 JSON。
在attachment
中,您需要再次添加一个attachment
。
JSON 会像下面这样。
{
"total":2233,
"issues":[
{
"id":"13598",
"key":"ID-2368",
"fields":{
"attachment":{
"**attachment**":[
{
"id":"11122"
}
]
}
}
}
]
}