Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java for Android
  • Android Studio
  • Android Kotlin
  • Kotlin
  • Flutter
  • Dart
  • Android Project
  • Android Interview
Open In App
Next Article:
Difference Between GSON and JSON in Android
Next article icon

Difference Between GSON and JSON in Android

Last Updated : 13 Sep, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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. 

  • It is used while writing JavaScript-based applications that include browser extensions and websites.
  • The JSON format is used for serializing and transmitting structured data over a network connection.
  • It is primarily used to transmit data between a server and web applications.
  • Web services and APIs use JSON format to provide public data.
  • It can be used with modern programming languages.

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?

  • It provides simple toJson() and fromJson() methods to convert Java objects to JSON and vice-versa
  • It allows pre-existing unmodifiable objects to be converted to and from JSON
  • It also provides extensive support for Java Generics
  • It allows custom representations for objects
  • GSON supports arbitrarily complex objects (with deep inheritance hierarchies and extensive use of generic types)
  • GSON is a standardized library that is managed by Google
  • It is a reliable, fast, and efficient extension of the Java standard library.
  • This library is highly optimized

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

Java
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;        }    } } 

Next Article
Difference Between GSON and JSON in Android

N

nakshatragupta007
Improve
Article Tags :
  • Difference Between
  • Android

Similar Reads

    Difference Between JSON and CSV
    JSON: JSON refers to JavaScript Object Notation. It is a language-independent, human-readable language used for its simplicity and is most commonly used in web-based applications. The JSON extensions end with a .json. JSON is a user-friendly substitute for XML as it is lightweight and easy to read.
    2 min read
    Difference between GPS and GPRS
    GPS and GPRS are two widely used mobile communication technologies, GPS(Global Positioning System). is used for the satellite based navigation systems and mapping whereas GPRS(General Packet Radio Service). is used for video calling, Email accessing, multimedia messaging etc. Let us explore each in
    4 min read
    Difference between GSM and GPRS
    In mobile communication there are two commanly used terms are GSM, GPRS. Both have distinct roles and functions based on the principles exhibited by them. In this article we will be discussed difference between GSM and GPRS.What is Global Systems for Mobile (GSM)?GSM is a cellular standard for mobil
    7 min read
    Difference Between YAML and JSON
    YAML and JSON are data serialization formats. YAML is human-readable, uses indentation, supports comments, and is ideal for configuration files. JSON is compact, machine-readable, lacks comment support, and is commonly used in APIs and data exchange.YAMLYAML is a light-weight, human-readable data-re
    2 min read
    Difference between GSM and LTE
    When you study mobile communication technologies, you will find two kinds of terms. They are the GSM (Global System for Mobile Communications) and LTE (Long-Term Evolution). These elements play a crucial role in mobile-to-mobile communication and SIM card operation. However, there are plenty of diff
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences