Understanding JSON and using GSON in Android with Kotlin and Android Studio

preview_player
Показать описание

In this video you will learn what JSON is and how to use GSON in Android Studio.

The sharing of data over the Internet is a prevalent task. Nowadays, almost every application has some features to exchange the data with the server over the Internet.

The details send and receive from the server can be in XML or JSON format. When you want to use the data store using these file formats in the database, you must apply some parsing of data in your Android Application. For the JSON file, you have to use the JSON parsing, and for XML files, you can use the XML parsing. In this article, you will learn how to parse the JSON format data and use it in our Android Application. So, let’s get started with JSON parsing.

What is JSON?

JSON stands for JavaScript Object Notation. The JSON is used to exchange the data from the server to the desired place over the Internet. The XML parsing is more complicated as compared to JSON parsing.

JSON is structured, lightweight, and an independent data exchange format used to parse data. JSON is also language-independent. JSON uses the syntax of JavaScript, which is limited to text. Apart from this, nowadays, most of the APIs available to us is sending data in the form of JSON and receiving that data from API, you have to do JSON parsing, and it is straightforward to do JSON parsing.

To manipulate JSON data and to play with JSON, Android provides four classes. These classes are:

JSONObject
JSONArray
JSONStringer
JSONTokenizer
JSON Structure

JSON uses two types of brackets which are as below:

[] – JSON uses square brackets to declare an Array's elements in JSON.
{} – JSON uses the curly brackets to create JSON objects elements.

JSON has the following types of structures that generally used, which are as below:

JSON Array

The elements present the list of values in the ([...]), known as Arrays. Inside the JSON array, you can put values like Integer, Boolean, String, Float, and also you can put some other JSON array or JSON object in a particular JSON array. 

JSON Objects

The elements present inside the curly ({...}) brackets are known as Objects. The JSON object contains key/value pairs like the Hash map. In the JSON object, we can also use nested JSONObjects, which are very commonly used nowadays. 

JSON Key-Value

The values can be in any primitive data type and are the value corresponding to a particular key. For example: "Username":"Denis Panjuta". Here the Username is key and the value corresponding is "Denis Panjuta".

JSON Parsing Functions

This is a list of functions in android which can be used for JSON Parsing:

get(int index): It is used to get the value of the object type present in the JSON array. It throws an exception if there is no JSONObject in the JSON array.
getType(int index): It gets the value of any of the mentioned types present in the JSON array. The type can be String, Int, Boolean, Long, Double, JSONArray, or JSONObject. 
length(): It is used to get the length of the array.
opt(int index): It returns the Object Type value in the JSONArray on a particular index.
optType(int index ): It returns the value of the mentioned types in the JSONArray on a particular index.
JSON Parsing in Android

You have learned the concept of JSON Parsing and its functions. Now, let’s implement the concept in the android project.

Parse the JSON using GSON

Now, you have seen the JSON parsing with the functions and which we need to do it manually line by line.

Here I will show you how to do it with a third-party library. There are many libraries that are used to parse JSON objects with the help of the data model class.

I will show you the third party library GSON which is quite popular. You can check out the link for more details.

Alright. Now you understand what JSON is and how you can use GSON in order to save loads of time when working with JSON.

This video is about:
- JSON in Android
- JSON in Android Studio Tutorial
- Using JSON in Android Studio
And of course, using GSON in android studio
parsing JSON using GSON
GSON tutorial Kotlin
GSON tutorial Android
You will see how to use GSON in android studio.
You will learn the basics of JSON
And how to parse JSON in android studio
also how to parse JSON using GSON in android
and of course how to parse JSON using GSON
So you will learn all the Parsing basics in android studio
This is a JSON tutorial for beginners which will cover JSON fundamentals in Kotlin
Рекомендации по теме
Комментарии
Автор

Bro geil das du wieder auf youtube was machst .. back to the roots :) mach aufjedenfall weiter hier :)

klaushopf
Автор

That was very elegant code, man! And the sound quality is brilliant! English is also very pleasent to understand! Great content!

KonstantinGanchin
Автор

Thanks a lot, my teacher. You are a good man!

chalot
Автор

Amazing explanation pretty good lesson, thanks man!

MrJQd
Автор

Please put more content on android. You are helping us a lot.

sanjogpanda
Автор

this used to be easy in webdevelopment lol. didn't expect GSON kotlin tutorials to be this long. btw, still good tutorial :)

adrilseanbartolome
Автор

so much helpful. thank you for your hard work to post such an excellent video. sir do you have the same video while parsing JSON from website. now you are fetching from the asset folder in the android main folder. I want the same code but only fetching the json data from the website. please send the code or guide me on how to fetch the JSON for this project from the website. Thank you in advance

juhanamkedirakumer
Автор

Another suggestion. Using JSON to prepare data in Unity games. Does JSON integrates with C# like in python?

diggerfdf
Автор

What if we have multiple arrays, how we can fetch those array names and display array content when we click on particular array object.
How sort arrays in json alphabetical order.

unknownurs
Автор

Sir can you make a tutorial on making music player app?

bikramchettri
Автор

Started learning all this about two months ago. Really difficult when things like synthetic x has changed so much and all the guides are showing an older way of connecting view. Even stuff only 6 months old has a ton of depracated concepts.

jacecrawford
Автор

Sir can you make a video on expandable listview? fetching nested data from json file using gson in kotlin?

samkitsanghavi
Автор

Hello, found this video. Well explained but how would do this in 2022, with Jetpack Compose?

tmjromao
Автор

I have several errors, although I did everything the same, could you help me. Unresolved reference: rvUsersList, Unresolved reference: Gson, Unresolved reference: LinearLayoutManager, Unresolved reference: rvUsersList, Unresolved reference: JSONException, Unresolved reference: Charset, Unresolved reference: IOException

Jesus-ezsj
Автор

One of my apps is developed by Kotlin language. After fetching my Data from API by using the retrofit library in my app then I am facing a memory problem on my virtual device.

md.shorifulhasan
Автор

Sir is this video included in your 45 hour long course on Android Development on Udemy ?

alokapatel
Автор

I can't seem to access assets.open() method it returns unresolved reference. tried importing packages but still won't cut .

vanfrancisco
Автор

Hey, i'm having an issue at 20:38 where my tv_name are all red and the holder.tvName.text above that code is also red. Well, the text part is. Not very experienced when it comes to kotlin and android studio and I can't seem to figure what the issue is. Any suggestions?

dantegraphics
Автор

if i try to do userlisst[i].name the whole app crashes

Bubatzo
Автор

i have no charset class, please dont skip as much stuff

Bubatzo