英文:
Deserializing JSON in Java using Jackson
问题
这是您提供的JSON片段,其中包含了一些Java类的代码。您的代码似乎是用于反序列化JSON数据的。以下是这些代码的翻译部分:
{
"total": 2236,
"issues": [{
"id": "10142",
"key": "ID-2",
"fields": {
"attachment": [{
"id": "11132"
}]
}
}]
}
这是我试图反序列化的JSON示例片段。
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;
}
请注意,上述代码是您提供的Java类的翻译部分,用于反序列化JSON数据。如果您有关于这些类或代码的具体问题,请随时提出。
英文:
I have the following sample fragment of JSON I am trying to deserialize.
{
"total": 2236,
"issues": [{
"id": "10142",
"key": "ID-2",
"fields": {
"attachment": [{
"id": "11132"
}]
}
}]
}
I can deserialize the data up to id and key, but cannot deserialize attachments that is in fields. My attachment class is always null
Here's my code.
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;
}
答案1
得分: 2
在你的JSON中,attachment
是一个数组,而不是一个对象。
你不需要一个名为 Attachments
的类,只需按照以下方式修改 Fields
:
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Fields {
@JsonProperty
private List<Attachment> attachment;
}
英文:
In your JSON, attachment
is an array, not an object.
You don't need an Attachments
class, just modify Fields
this way:
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Fields {
@JsonProperty
private List<Attachment> attachment;
}
答案2
得分: 1
将Java类中的每个变量都视为JSON中的一个属性。 "Attachments" 不在JSON文件中。 您应该能够删除它并更改Fields
类中的变量定义。
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Fields {
@JsonProperty
private List<Attachment> attachment;
}
英文:
Think of each variable in your Java classes as corresponding to an attribute in the JSON. "Attachments" is not in the JSON file. You should be able to remove it and change the variable definition in the Fields
class.
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Fields {
@JsonProperty
private List<Attachment> attachment;
}
答案3
得分: 0
如果你不想改变类结构,那么你可以修改JSON。
在 attachment
中,你需要再次添加一个 attachment
。
JSON 将如下所示。
{
"total": 2233,
"issues": [
{
"id": "13598",
"key": "ID-2368",
"fields": {
"attachment": {
"**attachment**": [
{
"id": "11122"
}
]
}
}
}
]
}
英文:
If you don't want to change class structure then, you can modify JSON.
In attachment
, you need to add again an attachment
.
JSON would be like below.
{
"total":2233,
"issues":[
{
"id":"13598",
"key":"ID-2368",
"fields":{
"attachment":{
"**attachment**":[
{
"id":"11122"
}
]
}
}
}
]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论