Deserializing Json to a Java Object Using Google’s Gson Library
Join the DZone community and get the full member experience.
Join For Free
javascript object notation (json) is fast becoming the de facto standard or format for transferring, sharing and passing around data. be it on the web, rest service, a remote procedure call or even an ajax request. json is light weight with little memory footprint when compared to an xml.
the content of a json string in its raw form when observed looks gibberish. to make the content usable it needs to be deserialized or converted to a useable form usually a java object (pojo) or an array or list of objects depending on the json content.
a typical json string is as shown below
there are a lot of frameworks for deserializing json to a java object such as json-rpc , gson , flexjson and a whole lots of other open source libraries. of all the libraries mentioned i would in this blog post demonstrate how to use google-gson library to deserialize a json string to a java object. you can download the gson library from https://code.google.com/p/google-gson/ .
to have the json string deserialized, a java object must be created that has the same fields names with the fields in the json string. there is a website that provides a service for viewing the content of a json string in a tree like manner. http://jsonviewer.stack.hu
paste the json string in the text tab
and view the fields and the content from the viewer tab
i would deserialize a json string that contains address details to an address pojo, the address object follows the structure as seen from the json tree view above.
to perform the deserialization with gson is easy, create pojo classes to hold your data, import the packages com.google.gson.gson and com.google.gson.gsonbuilder, to your project. then create and instance of the gson class and then perform the deserialization as shown below.
voila, you have your json deserialized!
the source code listing is below.
the content of a json string in its raw form when observed looks gibberish. to make the content usable it needs to be deserialized or converted to a useable form usually a java object (pojo) or an array or list of objects depending on the json content.
a typical json string is as shown below
{"city":"jos","country":"nigeria","housenumber":"13","lga":"jos south",
"state":"plateau","streetname":"jonah jann","village":"bukuru","ward":"1"}
there are a lot of frameworks for deserializing json to a java object such as json-rpc , gson , flexjson and a whole lots of other open source libraries. of all the libraries mentioned i would in this blog post demonstrate how to use google-gson library to deserialize a json string to a java object. you can download the gson library from https://code.google.com/p/google-gson/ .
to have the json string deserialized, a java object must be created that has the same fields names with the fields in the json string. there is a website that provides a service for viewing the content of a json string in a tree like manner. http://jsonviewer.stack.hu
paste the json string in the text tab
and view the fields and the content from the viewer tab
i would deserialize a json string that contains address details to an address pojo, the address object follows the structure as seen from the json tree view above.
public class address{
private string city;
private string country;
private string housenumber;
private string lga;
private string state;
private string streetname;
private string village;
private string ward;
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;
}
public string gethousenumber() {
return housenumber;
}
public void sethousenumber(string housenumber) {
this.housenumber = housenumber;
}
public string getlga() {
return lga;
}
public void setlga(string lga) {
this.lga = lga;
}
public string getstate() {
return state;
}
public void setstate(string state) {
this.state = state;
}
public string getstreetname() {
return streetname;
}
public void setstreetname(string streetname) {
this.streetname = streetname;
}
public string getvillage() {
return village;
}
public void setvillage(string village) {
this.village = village;
}
public string getward() {
return ward;
}
public void setward(string ward) {
this.ward = ward;
}
@override
public string tostring() {
return "address [city=" + city + ", country=" + country
+ ", housenumber=" + housenumber + ", lga=" + lga + ", state="
+ state + ", streetname=" + streetname + ", village=" + village
+ ", ward=" + ward + "]";
}
}
to perform the deserialization with gson is easy, create pojo classes to hold your data, import the packages com.google.gson.gson and com.google.gson.gsonbuilder, to your project. then create and instance of the gson class and then perform the deserialization as shown below.
gson gson = new gsonbuilder().create();
address address=gson.fromjson(json, address.class);
voila, you have your json deserialized!
the source code listing is below.
package jsondeserializer
import com.google.gson.gson;
import com.google.gson.gsonbuilder;
public class tester {
public static void main(string[] args) {
string json ="{\"city\":\"jos\",\"country\":\"nigeria\",\"housenumber\":\"13\",\"lga\":\"jos south\",\n" +
"\"state\":\"plateau\",\"streetname\":\"jonah jann\",\"village\":\"bukuru\",\"ward\":\"1\"}";
gson gson = new gsonbuilder().create();
address address=gson.fromjson(json, address.class);
system.out.println(address.tostring());
}
}
Object (computer science)
Gson
Java (programming language)
Library
Strings
Data Types
Published at DZone with permission of Ayobami Adewole, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments