Open In App

Difference Between GSON and JSON in Android

JSON stands for JavaScript Object Notation. It is a text format for storing and transporting data. JSON is “self-describing” and easy to understand. It is a lightweight data-interchange format. JSON is used to send data between computers as it is language-independent. Code for reading and generating JSON exists in many programming languages. The JSON format was originally specified by Douglas Crockford.

On the other hand, 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. It is an open-source library developed by Google.



Note: GSON is not an officially supported Google product.

Uses of JSON and GSON

Why do we use JSON?

The JSON format is syntactically similar to the code for creating JavaScript objects. Because of this, a JavaScript program can easily convert JSON data into JavaScript objects. Since the format is text only, JSON data can easily be sent between computers and used by any programming language. 



JSON is a data format that is human-readable and supported by a wide variety of languages.

An example of JSON encoded data:

{
"name":"Amit",
"age":25,
"interests":["singing", "reading", "playing cricket"],
"favorites":{
             "color":"red",
             "cricketer":"Virat, Dhoni, Rohit"
            }
}

Individual examples of XML and JSON :

JSON

{
 "company": GeeksforGeeks,
 "established": "2007",
 "About": Best platform to learn DSA
}

XML

<GFG>
    <company>GeeksforGeeks</company>
    <established>2007</established>
    <About> Best platform to learn DSA</About>
</GFG>

Checkout JSON Parsing in Android to understand a few important functions related to JSON in Android.

Why do we use GSON?

How to Download GSON in an Android Application

Gradle:

dependencies {
    implementation 'com.google.code.gson:gson:2.9.1'
}

Maven:

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.9.1</version>
</dependency>

Full GSON Hello World Example

You can copy and paste this example into your IDE as long as you have GSON installed and running




import com.google.gson.Gson;
  
public class GsonHelloWorld {
   public static void main(String[] args) {
       // init class
       Place place = new Place();
       place.setName("World");
  
       Human human = new Human();
       human.setMessage("Hi");
       human.setPlace(place);
  
       // convert to json
       Gson gson = new Gson();
       String jsonString = gson.toJson(human);
       // print "json {"message":"Hi","place":{"name":"World"}}"
       System.out.println("json " + jsonString);
  
       // convert from json
       Human newHuman = gson.fromJson(jsonString, Human.class);
       newHuman.say(); // print "Hi , World!"
   }
  
   private static class Human {
       private String message;
       private Place place;
  
       public String getMessage() {
           return message;
       }
  
       public void setMessage(String message) {
           this.message = message;
       }
  
       public Place getPlace() {
           return place;
       }
  
       public void setPlace(Place place) {
           this.place = place;
       }
  
       public void say() {
           System.out.println();
           System.out.println(getMessage() + " , " + getPlace().getName() + "!");
       }
   }
  
   private static class Place {
       private String name;
  
       public String getName() {
           return name;
       }
  
       public void setName(String name) {
           this.name = name;
       }
   }
}


Article Tags :