Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!
Deniel Kerr
Founder. Opencart
Top Partners
In this blog, we are going to tell about the google’s GSON library. How to parse JSON to Java objects and vice-versa.
Gson is a Java library that can be used to parse the Java Objects into their JSON and vice-versa. It can also be used to convert a JSON string to an equivalent Java object.
Google developed GSON for internal use but open sourced it later.
For this purpose, Gson provides several built-in serializers and deserializers.
Serializers: A serializer allows to convert a Json string to corresponding Java Model or objects.
Deserializer: A deserializer allows to convert from Java Model to a JSON representation. You can also configure Gson to use custom representations of your objects.
Gson allows serializing an Collections of objects of the same type.
Collections
For use Gson in a Gradle build, add compile 'com.google.code.gson:gson:2.6.2' 1 compile 'com.google.code.gson:gson:2.6.2' or a later version as dependency to your build.gradl e build file.
For use Gson in a Gradle build, add
or a later version as dependency to your build.gradl e build file.
There are two methods to create an Gson instance:
Gson
new Gson()
GsonBuilder
create().
We can simply create the GSON by below method:
Here is an example of creating a GsonBuilder and calling:create()
create()
Using a GsonBuilder allows you to set configuration options on the GsonBuilder before creating the Gsonobject.
GSON can parse JSON into Java objects using the fromJson() method of the Gson object. Below is a GSON example of parsing JSON into a Java object or Model:
fromJson()
Below is how the Cloth model class looks:
Cloth
GSON can also generate JSON from Java objects. You do so via the Gson object. To generate JSON you call the toJson() method of the Gson object. Here is an example of generating JSON from a Java object with GSON:
toJson()
Here is an example of the compact JSON output from a default Gson instance:
However, this compact JSON can be hard to read. Therefore GSON offers a pretty printing option where the JSON is printed so it is more readable in a text editor.
Here is an example of how to create a Gson instance with pretty print enabled:
Here is an example of how the same JSON would look pretty-printed:
Thank you for reading it, maybe it will help you to understand the Basis GSON.
Happy Coding, Stay Super and Stay Cool.
Your email address will not be published. Required fields are marked*
Name*
Email*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
We use cookies to personalize your experience. By continuing to visit this website you agree to our use of cookies. Learn more about privacy policy
Webkul is a truly trusted and supported IT Company to develop business ideas for e-commerce. They provide professional support after deploying the solution to production and be responsible to act for fixing the reported issues or system errors. We highly encourage to deal with Webkul for business development.
Hussein Zawia
Co-founder, 24Dokan
USA
India
Global
Name
Email
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.