I'm trying to put JAXB annotations on POJO to produce following JSON (via moxy):
{
"apartmentNumber": "404",
"city/town": "SomeCity",
"state/province": "NoState",
"street": "1st Street"
}
Some fields contains slashes. When I'm trying to put annotation @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 treats such names as XPaths and creates following JSON
{
"apartmentNumber" : "404",
"city" : {
"town" : "SomeCity"
},
"state" : {
"province" : "NoState"
},
"street" : "1st Street"
}
Is there any way to escape slash / forbid moxy to treat slashes in element names?
The ObjectMapper class provided by Jackson API provides functionality for converting between Java Objects and JSON. If you are using Maven as a build tool in your project, then add the following dependency in your pom.xml file. Convert POJO to JSON and vice versa using Jackson API...!!!
Here's how you can convert your JSON string to JAVA objects, we will be using the converter and external libraries like Jackson objectmapper to parse our object. 1. Copy and paste your JSON in the first code editor and click "Convert" Make sure that your JSON object is not large (over 5MB) and is formatted.
If you want to serialize your Java object and map a field name in Java to a different property name in JSON, you can use @JsonGetter . Note that there's no @JsonProperty annotation. Also there's no getName () method. Instead, there's a getEmployeeName () method that's annotated with @JsonGetter ("name").
How jsonschema2pojo maps format values to Java types: When the extends rule is encountered in your JSON schema (to indicate that one type extends another), this will produce an extends relationship in your generated Java types. That extends value can be a schema given in-line, or referenced using $ref.
Instead of moxy U may try GSON, as it is producing the result.
The POJO is :
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;
}
}
The JSON generated is :
{
"SubscriberAddress": {
"street": "Demo Street",
"apartmentNumber": "Demo Apartment",
"city/town": "Demo City",
"state/province": "Demo State"
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With