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

  Рет қаралды 24,272

tutorialsEU

tutorialsEU

3 жыл бұрын

🚀 Android & Kotlin Masterclass - Become a serious Android app developer with our best-selling masterclass: bit.ly/3PaKdhS
In this video you will learn what JSON is and how to use GSON in Android Studio.
You can find the article with the code here: tutorials.eu/json-parsing-and...
Stay tuned and subscribe to tutorialsEU: goo.gl/rBFh3x
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.
Add the below dependency to your app-level build.gradle file to use GSON.
implementation 'com.google.code.gson:gson:2.8.6'
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

Пікірлер: 45
@tutorialsEU
@tutorialsEU 3 жыл бұрын
🚀 Android & Kotlin Masterclass - Become a serious Android app developer with our best-selling masterclass: bit.ly/3PaKdhS
@KonstantinGanchin
@KonstantinGanchin 2 жыл бұрын
That was very elegant code, man! And the sound quality is brilliant! English is also very pleasent to understand! Great content!
@chalot001
@chalot001 3 жыл бұрын
Thanks a lot, my teacher. You are a good man!
@klaushopf2101
@klaushopf2101 3 жыл бұрын
Bro geil das du wieder auf youtube was machst .. back to the roots :) mach aufjedenfall weiter hier :)
@MrJQ3d
@MrJQ3d 2 жыл бұрын
Amazing explanation pretty good lesson, thanks man!
@sanjogpanda2597
@sanjogpanda2597 3 жыл бұрын
Please put more content on android. You are helping us a lot.
@anthonyburke5147
@anthonyburke5147 2 жыл бұрын
Excellent video! Thanks heaps
@adrilseanbartolome9102
@adrilseanbartolome9102 2 жыл бұрын
this used to be easy in webdevelopment lol. didn't expect GSON kotlin tutorials to be this long. btw, still good tutorial :)
@forooghvarmazyar9759
@forooghvarmazyar9759 2 жыл бұрын
thanks a lot :)
@juhanamkedirakumer4730
@juhanamkedirakumer4730 3 жыл бұрын
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
@unknownurs2367
@unknownurs2367 2 жыл бұрын
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.
@kingfisher3791
@kingfisher3791 3 жыл бұрын
Plz do video on motion layout
@hajyosrihosni786
@hajyosrihosni786 3 жыл бұрын
thx
@dkmarwat5806
@dkmarwat5806 3 жыл бұрын
good sir
@diggerfdf
@diggerfdf 3 жыл бұрын
Another suggestion. Using JSON to prepare data in Unity games. Does JSON integrates with C# like in python?
@bikramchettri9405
@bikramchettri9405 3 жыл бұрын
Sir can you make a tutorial on making music player app?
@Jesus-ez7sj
@Jesus-ez7sj 2 жыл бұрын
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
@tmjromao
@tmjromao 2 жыл бұрын
Hello, found this video. Well explained but how would do this in 2022, with Jetpack Compose?
@DigiWongaDude
@DigiWongaDude Жыл бұрын
Let me know if you found any help please?
@jacecrawford6015
@jacecrawford6015 3 жыл бұрын
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.
@keisjones1049
@keisjones1049 2 жыл бұрын
i had to add view binding
@samkitsanghavi4927
@samkitsanghavi4927 2 жыл бұрын
Sir can you make a video on expandable listview? fetching nested data from json file using gson in kotlin?
@tutorialsEU
@tutorialsEU 2 жыл бұрын
This sounds like quite a mouth full. Will check it out, but can't promise it.
@md.shorifulhasan3149
@md.shorifulhasan3149 2 жыл бұрын
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.
@alokapatel8159
@alokapatel8159 3 жыл бұрын
Sir is this video included in your 45 hour long course on Android Development on Udemy ?
@tutorialsEU
@tutorialsEU 3 жыл бұрын
Hi Aloka, for some reason I see this message just now. Yes it is included
@dantegraphics6259
@dantegraphics6259 3 жыл бұрын
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?
@dantegraphics6259
@dantegraphics6259 3 жыл бұрын
I found the issue. in my build.gradle i didnt have an id 'kotlin-android-extensions' so i added it in as a plugin below the others. This worked for me so if anyone has a similar issue here is a potential fix
@tutorialsEU
@tutorialsEU 3 жыл бұрын
Hi Dante. Yes that's one way to solve it. I made a video on the three ways and which one is the best: kzfaq.info/get/bejne/aq2Imc96ydjQpac.html
@dantegraphics6259
@dantegraphics6259 3 жыл бұрын
@@tutorialsEU Ty! By the way, do you have a tutorial on sorting these lists/recycler views? I'm making an app for my school assessment task and i need to incorporate bubble sorts, insertion sorts and selection sorts and tbh I don't really know how to go about it.
@dantegraphics6259
@dantegraphics6259 3 жыл бұрын
Another thing, I would like to have the user add their own elements into the JSON array. From what I understand, JSON file is read only so it needs to be saved to internal storage and then can be edited. What I really want to do is make a button that can allow the user to 'add' their own user to the list. Is there a way to do this after following this tutorial?
@user-jj3cz9gw3x
@user-jj3cz9gw3x 7 ай бұрын
This works. Thank you@@dantegraphics6259
@vanfrancisco4573
@vanfrancisco4573 2 жыл бұрын
I can't seem to access assets.open() method it returns unresolved reference. tried importing packages but still won't cut .
@nihalouhaouada4857
@nihalouhaouada4857 2 жыл бұрын
You have to write : context.assets.open(file)
@Bubatzo
@Bubatzo Жыл бұрын
if i try to do userlisst[i].name the whole app crashes
@hermanheunis9354
@hermanheunis9354 2 жыл бұрын
Gson fun starts at 38:04
@Bubatzo
@Bubatzo Жыл бұрын
i have no charset class, please dont skip as much stuff
@12345charliebrown
@12345charliebrown 3 жыл бұрын
import json obj = json.loads(s) dont really need an hour long video to use json in python.
@12six69
@12six69 2 жыл бұрын
takes more time
@12345charliebrown
@12345charliebrown 2 жыл бұрын
@@12six69 my point being why isn't there a defacto json serializer built into standard Kotlin library?
@santi7761
@santi7761 2 жыл бұрын
amazing, it's a worst tutorial
MVVM в Android на практике
41:32
Тимофей Коваленко
Рет қаралды 47 М.
Retrofit Tutorial #1 - GET Simple JSON [Retrofit with Android Studio]
29:06
Мы никогда не были так напуганы!
00:15
Аришнев
Рет қаралды 6 МЛН
버블티로 체감되는 요즘 물가
00:16
진영민yeongmin
Рет қаралды 120 МЛН
Smart Sigma Kid #funny #sigma #comedy
00:25
CRAZY GREAPA
Рет қаралды 19 МЛН
Khó thế mà cũng làm được || How did the police do that? #shorts
01:00
Kotlin Coroutine (High-quality Course)
2:07:37
Smartherd
Рет қаралды 98 М.
Web API Android Studio Kotlin Tutorial | REST
10:23
Code With Cal
Рет қаралды 19 М.
Kotlin Youtube - How to Quickly Fetch Parse JSON with OkHttp and Gson (Ep 2)
26:14
Learn JSON in 10 Minutes
12:00
Web Dev Simplified
Рет қаралды 3,1 МЛН
JSON Parsing using Volley Android [ Kotlin ]
17:01
Programming Guru
Рет қаралды 7 М.
The Ultimate Retrofit Crash Course
34:49
Philipp Lackner
Рет қаралды 90 М.
Мы никогда не были так напуганы!
00:15
Аришнев
Рет қаралды 6 МЛН