在Java中使用Jackson反序列化JSON。

huangapple go评论78阅读模式
英文:

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&lt;Attachment&gt; 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&lt;Attachment&gt; 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.

{
   &quot;total&quot;:2233,
   &quot;issues&quot;:[
      {
         &quot;id&quot;:&quot;13598&quot;,
         &quot;key&quot;:&quot;ID-2368&quot;,
         &quot;fields&quot;:{
            &quot;attachment&quot;:{
               &quot;**attachment**&quot;:[
                  {
                     &quot;id&quot;:&quot;11122&quot;
                  }
               ]
            }
         }
      }
   ]
}

huangapple
  • 本文由 发表于 2020年7月22日 21:22:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63035201.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定