Last active
December 28, 2015 09:19
-
-
Save tomlins/7478031 to your computer and use it in GitHub Desktop.
An example of creating a JSON string representation of an object with a nested object using the Gson library from Google. You will need to add the google-gson library as a dependency. Available here -> https://code.google.com/p/google-gson/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.tomlins.testapplication.json; | |
import com.google.gson.*; | |
public class JavaObjToGSONStringExample { | |
public static void main(String... args) { | |
JavaObjToGSONStringExample app = new JavaObjToGSONStringExample(); | |
System.out.println(app.toGSONStringExample()); | |
} | |
public String toGSONStringExample() { | |
// Create a GSon object | |
Gson gson = new Gson(); | |
// Create a simple Employee object | |
Employee emp = new Employee(); | |
emp.setName("Jason"); | |
emp.setPosition("Java Developer"); | |
emp.setSalary(1000); | |
// Create a simple Address object for the Employee object | |
Address empAddress = new Address(); | |
empAddress.setHouseNumber(10); | |
empAddress.setStreet("Java Street"); | |
empAddress.setSuburb("Grange"); | |
empAddress.setCity("Brisbane"); | |
empAddress.setCountry("Australia"); | |
// Set the employees address | |
emp.setAddress(empAddress); | |
// Get our GSon string representation of the Employee object | |
String sGobj = gson.toJson(emp); | |
return sGobj; | |
} | |
/** | |
* Simple Employee POJO | |
*/ | |
public class Employee { | |
private String name; | |
private String position; | |
private int salary; | |
private Address address; | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getPosition() { | |
return position; | |
} | |
public void setPosition(String position) { | |
this.position = position; | |
} | |
public int getSalary() { | |
return salary; | |
} | |
public void setSalary(int salary) { | |
this.salary = salary; | |
} | |
public Address getAddress() { | |
return address; | |
} | |
public void setAddress(Address address) { | |
this.address = address; | |
} | |
} | |
/** | |
* Simple Address POJO | |
*/ | |
public class Address { | |
private int houseNumber; | |
private String street; | |
private String suburb; | |
private String city; | |
private String country; | |
public int getHouseNumber() { | |
return houseNumber; | |
} | |
public void setHouseNumber(int houseNumber) { | |
this.houseNumber = houseNumber; | |
} | |
public String getStreet() { | |
return street; | |
} | |
public void setStreet(String street) { | |
this.street = street; | |
} | |
public String getSuburb() { | |
return suburb; | |
} | |
public void setSuburb(String suburb) { | |
this.suburb = suburb; | |
} | |
public String getCity() { | |
return city; | |
} | |
public void setCity(String city) { | |
this.city = city; | |
} | |
public String getCountry() { | |
return country; | |
} | |
public void setCountry(String country) { | |
this.country = country; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment