Required JSON String with Array:
{"NAME":"USER","AGE":25,"COUNTRY":"INDIA",
"ADDRESS": [{"STREET":"xys","CITY":"Bangalore","POSTAL CODE":54321}]
}
CreateJson class having logic for creating and parsing JSON
import java.util.ArrayList;
import java.util.List;
public class CreateJson {
public static void main(String[] args) {
UserBean userData = new UserBean();
userData.setName("USER");
userData.setAge(25);
userData.setCountry("INDIA");
AddressBean address = new AddressBean();
address.setCity("Bangalore");
address.setPostalCode(54321);
address.setStreet("xys");
List<AddressBean> addressList = new ArrayList<AddressBean>();
addressList.add(address);
userData.setAddressList(addressList);
String jsonString = JsonUtil.getJSONString(userData);
System.out.println(jsonString);
//Method Call to Parse JsonString
parseMyJsonString(jsonString);
}
private static void parseMyJsonString(String jsonString) {
UserBean userBean = JsonUtil.fromJsonToObject(jsonString);
System.out.println(userBean.getAge());
List<AddressBean> addressList = userBean.getAddressList();
for (AddressBean list : addressList) {
System.out.println("City " + list.getCity());
System.out.println("Postal Code " + list.getPostalCode());
}
}
}
1. Create AddressBean.java
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.annotate.JsonSerialize;
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class AddressBean {
@JsonProperty("STREET")
private String street = null;
@JsonProperty("CITY")
private String city = null;
@JsonProperty("POSTAL CODE")
private int postalCode;
public AddressBean(){
}
public void setStreet(String street) {
this.street = street;
}
public String getStreet() {
return street;
}
public void setCity(String city) {
this.city = city;
}
public String getCity() {
return city;
}
public void setPostalCode(int postalCode) {
this.postalCode = postalCode;
}
public int getPostalCode() {
return postalCode;
}
}
2. Create a UserBean.java
import java.util.List;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.annotate.JsonSerialize;
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class UserBean {
@JsonProperty("NAME")
private String name = null;
@JsonProperty("AGE")
private int age = 0;
@JsonProperty("COUNTRY")
private String country = null;
//List of AddressBean
@JsonProperty("ADDRESS")
private List<AddressBean> addressList = null;
public UserBean(){
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setCountry(String country) {
this.country = country;
}
public String getCountry() {
return country;
}
public void setAddressList(List<AddressBean> addressList) {
this.addressList = addressList;
}
public List<AddressBean> getAddressList() {
return addressList;
}
}
3. Create a Util Class having logic for parsing and creating JSON of UserBean object
import java.io.IOException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
public final class JsonUtil {
/**
* Creates a JSON String
* @param bean
* @return String
*/
public static String getJSONString(UserBean bean) {
String jsonString = null;
try {
jsonString = createObjectMapper().writeValueAsString(bean);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jsonString;
}
/**
* Parse Json string to UserBean Object
* @param json
* @return UserBean
*/
public static UserBean fromJsonToObject(String json) {
UserBean responseObj = null;
try {
responseObj = createObjectMapper().readValue(json, UserBean.class);
} catch (Exception exception) {
exception.printStackTrace();
}
return responseObj;
}
/**
* @return ObjectMapper
*/
private static ObjectMapper createObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY,
true);
mapper.configure(DeserializationConfig.Feature.USE_JAVA_ARRAY_FOR_JSON_ARRAY,
true);
mapper.configure(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT,
true);
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS,
true);
return mapper;
}
}
*Using jackson-all-1.9.0.jar
*Comments and feedback are most welcomed, Thanks.
{"NAME":"USER","AGE":25,"COUNTRY":"INDIA",
"ADDRESS": [{"STREET":"xys","CITY":"Bangalore","POSTAL CODE":54321}]
}
CreateJson class having logic for creating and parsing JSON
import java.util.ArrayList;
import java.util.List;
public class CreateJson {
public static void main(String[] args) {
UserBean userData = new UserBean();
userData.setName("USER");
userData.setAge(25);
userData.setCountry("INDIA");
AddressBean address = new AddressBean();
address.setCity("Bangalore");
address.setPostalCode(54321);
address.setStreet("xys");
List<AddressBean> addressList = new ArrayList<AddressBean>();
addressList.add(address);
userData.setAddressList(addressList);
String jsonString = JsonUtil.getJSONString(userData);
System.out.println(jsonString);
//Method Call to Parse JsonString
parseMyJsonString(jsonString);
}
private static void parseMyJsonString(String jsonString) {
UserBean userBean = JsonUtil.fromJsonToObject(jsonString);
System.out.println(userBean.getAge());
List<AddressBean> addressList = userBean.getAddressList();
for (AddressBean list : addressList) {
System.out.println("City " + list.getCity());
System.out.println("Postal Code " + list.getPostalCode());
}
}
}
1. Create AddressBean.java
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.annotate.JsonSerialize;
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class AddressBean {
@JsonProperty("STREET")
private String street = null;
@JsonProperty("CITY")
private String city = null;
@JsonProperty("POSTAL CODE")
private int postalCode;
public AddressBean(){
}
public void setStreet(String street) {
this.street = street;
}
public String getStreet() {
return street;
}
public void setCity(String city) {
this.city = city;
}
public String getCity() {
return city;
}
public void setPostalCode(int postalCode) {
this.postalCode = postalCode;
}
public int getPostalCode() {
return postalCode;
}
}
2. Create a UserBean.java
import java.util.List;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.annotate.JsonSerialize;
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class UserBean {
@JsonProperty("NAME")
private String name = null;
@JsonProperty("AGE")
private int age = 0;
@JsonProperty("COUNTRY")
private String country = null;
//List of AddressBean
@JsonProperty("ADDRESS")
private List<AddressBean> addressList = null;
public UserBean(){
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setCountry(String country) {
this.country = country;
}
public String getCountry() {
return country;
}
public void setAddressList(List<AddressBean> addressList) {
this.addressList = addressList;
}
public List<AddressBean> getAddressList() {
return addressList;
}
}
3. Create a Util Class having logic for parsing and creating JSON of UserBean object
import java.io.IOException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
public final class JsonUtil {
/**
* Creates a JSON String
* @param bean
* @return String
*/
public static String getJSONString(UserBean bean) {
String jsonString = null;
try {
jsonString = createObjectMapper().writeValueAsString(bean);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jsonString;
}
/**
* Parse Json string to UserBean Object
* @param json
* @return UserBean
*/
public static UserBean fromJsonToObject(String json) {
UserBean responseObj = null;
try {
responseObj = createObjectMapper().readValue(json, UserBean.class);
} catch (Exception exception) {
exception.printStackTrace();
}
return responseObj;
}
/**
* @return ObjectMapper
*/
private static ObjectMapper createObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY,
true);
mapper.configure(DeserializationConfig.Feature.USE_JAVA_ARRAY_FOR_JSON_ARRAY,
true);
mapper.configure(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT,
true);
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS,
true);
return mapper;
}
}
*Using jackson-all-1.9.0.jar
*Comments and feedback are most welcomed, Thanks.