Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gson library in Android Studio

Tags:

android

gson

Can someone give me step by step guide to add the Gson library to an Android project?

I tried the JSON built-in library but that seems to be a bit tedious right now. I saw a couple of examples based on Gson, and that seems really easy.

like image 978
Venky Avatar asked May 05 '16 10:05

Venky


People also ask

What is Gson Library in Android?

Google's Gson library provides a powerful framework for converting between JSON strings and Java objects. This library helps to avoid needing to write boilerplate code to parse JSON responses yourself. It can be used with any networking library, including the Android Async HTTP Client and OkHttp.

How do I use Gson on Android?

Important Gson methods It must be used if the specified object is a generic type. For non-generic objects, use toJson(Object,Appendable) instead. This method deserializes the specified Json into an object of the specified type. This method is useful if the specified object is a generic type.

What is Gson library?

Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.


3 Answers

Add following dependency to build.gradle:

implementation 'com.google.code.gson:gson:2.8.7'

Or download the JAR file from Maven by clicking a release and finding the .jar file.

Replace 2.8.7 with the latest version from Maven.

Visit the GitHub repo for documentation and more.

like image 78
PEHLAJ Avatar answered Oct 18 '22 01:10

PEHLAJ


Read Google-gson

Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object.

Add the following line to your MODULE LEVEL build.gradle configuration:

dependencies {
     implementation 'com.google.code.gson:gson:2.8.8' // Old 2.8.6
}
like image 30
IntelliJ Amiya Avatar answered Oct 18 '22 01:10

IntelliJ Amiya


Use gradle dependencies to get the Gson in your project. Your application build.gradle should look like this-

dependencies {
  implementation 'com.google.code.gson:gson:2.8.2'
}
like image 7
D_Alpha Avatar answered Oct 18 '22 01:10

D_Alpha