通过moxy将字段名称中的斜杠转换为POJO为JSON

时间:2016-07-13 20:18:53

标签: java json jaxb moxy

我正在尝试将JAXB注释放在POJO上以生成以下JSON(通过moxy):

{
    "apartmentNumber": "404",
    "city/town": "SomeCity",
    "state/province": "NoState",
    "street": "1st Street"
}

某些字段包含斜杠。当我试图将注释@XmlElement(name =“city / town”)

@XMLRootElement
public class SubscriberAddress {
    private String street;
    private String apartmentNumber;

    @XMLElement(name="city/town")
    private String city;

    @XMLElement(name="state/province")
    private String state;
}

moxy将此类名称视为XPath并创建以下JSON

{
     "apartmentNumber" : "404",
     "city" : {
         "town" : "SomeCity"
     },
     "state" : {
         "province" : "NoState"
     },
     "street" : "1st Street"
}

有没有办法逃避斜线/禁止moxy来处理元素名称中的斜杠?

1 个答案:

答案 0 :(得分:1)

而不是moxy U可能会尝试GSON,因为它会产生结果。

POJO是:

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class SubscriberAddress {
    private String street;
    private String apartmentNumber;

    @XmlElement(name = "city/town")
    private String city;

    @XmlElement(name = "state/province")
    private String state;

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getApartmentNumber() {
        return apartmentNumber;
    }

    public void setApartmentNumber(String apartmentNumber) {
        this.apartmentNumber = apartmentNumber;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }
}

生成的JSON是:

{
  "SubscriberAddress": {
    "street": "Demo Street",
    "apartmentNumber": "Demo Apartment",
    "city/town": "Demo City",
    "state/province": "Demo State"
  }
}