代码之家  ›  专栏  ›  技术社区  ›  Sach

Jackson将XML/Bean转换为JSON数组

  •  0
  • Sach  · 技术社区  · 6 年前

    我用的是杰克森2.8.2。我有以下格式的XML:

    <descriptions>
        <description description1="1" description1Text="text1" />
        <description description1="6" description1Text="text2" description2="19" description2Text="Board Member (BRD)" />
    </descriptions>
    

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "Phone", propOrder = {
          "descriptions"
    }
    @XmlRootElement(name="phone")
     public class Phone extends PhoneBase {
    
     protected Descriptions descriptions;
    
     getter()
     setter();
     }
    

    @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;
    }
    }
    

        {
    "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)"
        }
    ]}
    

    有人能建议我如何做到这一点吗?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Sach    6 年前

    @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;
    }
    }