Following DTO can be used in parsing and
creating json.
package com.smoothexample.json;
import javax.xml.bind.annotation.XmlRootElement;
public class Address {
@Override
public String toString() {
return "Address [streetAddress=" + streetAddress + ", addressOptional=" + addressOptional + ", city=" + city
+ ", state=" + state + ", country=" + country + ", zip=" + zip + "]";
}
private String streetAddress;
private String addressOptional;
private String city;
private String state;
private String country;
private String zip;
public String getStreetAddress() {
return streetAddress;
}
public void setStreetAddress(String streetAddress) {
this.streetAddress = streetAddress;
}
public String getAddressOptional() {
return addressOptional;
}
public void setAddressOptional(String addressOptional) {
this.addressOptional = addressOptional;
}
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;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
}
Step 3:Creating and
Parsing Json - Example
Following application will first
convert an object to json. Then it will again convert the generated
json string to related java object. Alsojson encode and decode
properties are set in the method private static ObjectMapper
getObjectMapper() {..}