杰克逊将 XML/Bean 转换为 Json 数组
Jackson transforming XML/Bean into Json Array
(请注意,我只提供了一些代码片段来解释我的情况,这些并不是完整的对象)
我正在使用 Jackson2.8.2。我有以下格式的 xml:
<descriptions>
<description description1="1" description1Text="text1" />
<description description1="6" description1Text="text2" description2="19" description2Text="Board Member (BRD)" />
</descriptions>
我使用 JAXB 将此 XML 转换为 Java POJO,如下所示:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Phone", propOrder = {
"descriptions"
}
@XmlRootElement(name="phone")
public class Phone extends PhoneBase {
protected Descriptions descriptions;
getter()
setter();
}
说明CLass如下:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Descriptions", propOrder = {
"description"
})
public class Descriptions {
@XmlElement(required = true)
protected List<Description> description;
public List<Description> getDescription() {
if (description == null) {
description = new ArrayList<Description>();
}
return this.description;
}
}
当我使用 Jackson 将上面的 POJO 转换为 JSON 时,我得到下面的结果:
{
"descriptions": {
"description": [{
"description1": "1",
"description1_text": "text1"
}, {
"description1": "6",
"description1_text": "text2",
"description2": "19",
"description2_text": "Board Member (BRD)"
}
] } }
虽然我希望它如下所示:
{
"descriptions": [{
"description1": "1",
"description1_text": "text1"
}, {
"description1": "6",
"description1_text": "text2",
"description2": "19",
"description2_text": "Board Member (BRD)"
}
]}
请注意我不想要父节点,而是子节点表示为 JSON 数组,并且此键的名称是父节点的名称。
有人可以建议我如何实现这一目标吗?
谢谢
我使用 lexicore 的指针解决了我的问题,并将我的 phone 类更改如下:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Phone", propOrder = {
"description"
}
@XmlRootElement(name="phone")
public class Phone
extends PhoneBase
{
@XmlElementWrapper(name = "descriptions")
@JsonProperty(value = "descriptions")
@XmlElement(required = true)
protected List<Description> description;
public List<Description> getDescription() {
if (description == null) {
description = new ArrayList<Description>();
}
return this.description;
}
}
(请注意,我只提供了一些代码片段来解释我的情况,这些并不是完整的对象) 我正在使用 Jackson2.8.2。我有以下格式的 xml:
<descriptions>
<description description1="1" description1Text="text1" />
<description description1="6" description1Text="text2" description2="19" description2Text="Board Member (BRD)" />
</descriptions>
我使用 JAXB 将此 XML 转换为 Java POJO,如下所示:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Phone", propOrder = {
"descriptions"
}
@XmlRootElement(name="phone")
public class Phone extends PhoneBase {
protected Descriptions descriptions;
getter()
setter();
}
说明CLass如下:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Descriptions", propOrder = {
"description"
})
public class Descriptions {
@XmlElement(required = true)
protected List<Description> description;
public List<Description> getDescription() {
if (description == null) {
description = new ArrayList<Description>();
}
return this.description;
}
}
当我使用 Jackson 将上面的 POJO 转换为 JSON 时,我得到下面的结果:
{
"descriptions": {
"description": [{
"description1": "1",
"description1_text": "text1"
}, {
"description1": "6",
"description1_text": "text2",
"description2": "19",
"description2_text": "Board Member (BRD)"
}
] } }
虽然我希望它如下所示:
{
"descriptions": [{
"description1": "1",
"description1_text": "text1"
}, {
"description1": "6",
"description1_text": "text2",
"description2": "19",
"description2_text": "Board Member (BRD)"
}
]}
请注意我不想要父节点,而是子节点表示为 JSON 数组,并且此键的名称是父节点的名称。 有人可以建议我如何实现这一目标吗?
谢谢
我使用 lexicore 的指针解决了我的问题,并将我的 phone 类更改如下:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Phone", propOrder = {
"description"
}
@XmlRootElement(name="phone")
public class Phone
extends PhoneBase
{
@XmlElementWrapper(name = "descriptions")
@JsonProperty(value = "descriptions")
@XmlElement(required = true)
protected List<Description> description;
public List<Description> getDescription() {
if (description == null) {
description = new ArrayList<Description>();
}
return this.description;
}
}