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

Поделиться
HTML-код
  • Опубликовано: 10 сен 2020
  • 🚀 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 года назад +3

    🚀 Android & Kotlin Masterclass - Become a serious Android app developer with our best-selling masterclass: bit.ly/3PaKdhS

  • @KonstantinGanchin
    @KonstantinGanchin 2 года назад +2

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

  • @klaushopf2101
    @klaushopf2101 3 года назад

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

  • @chalot001
    @chalot001 3 года назад

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

  • @MrJQ3d
    @MrJQ3d 3 года назад

    Amazing explanation pretty good lesson, thanks man!

  • @anthonyburke5147
    @anthonyburke5147 2 года назад

    Excellent video! Thanks heaps

  • @sanjogpanda2597
    @sanjogpanda2597 3 года назад

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

  • @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 :)

  • @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.

  • @forooghvarmazyar9759
    @forooghvarmazyar9759 2 года назад

    thanks a lot :)

  • @Jesus-ez7sj
    @Jesus-ez7sj 2 года назад +1

    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

  • @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.

  • @hajyosrihosni786
    @hajyosrihosni786 3 года назад

    thx

  • @juhanamkedirakumer4730
    @juhanamkedirakumer4730 3 года назад +1

    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

  • @kingfisher3791
    @kingfisher3791 3 года назад +1

    Plz do video on motion layout

  • @diggerfdf
    @diggerfdf 3 года назад

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

  • @bikramchettri9405
    @bikramchettri9405 3 года назад +1

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

  • @dkmarwat5806
    @dkmarwat5806 3 года назад

    good sir

  • @vanfrancisco4573
    @vanfrancisco4573 2 года назад +1

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

  • @Bubatzo
    @Bubatzo Год назад

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

  • @jacecrawford6015
    @jacecrawford6015 3 года назад +2

    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.

  • @tmjromao
    @tmjromao 2 года назад +1

    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?

  • @dantegraphics6259
    @dantegraphics6259 3 года назад +1

    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 года назад +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 года назад +2

      Hi Dante. Yes that's one way to solve it. I made a video on the three ways and which one is the best: ruclips.net/video/4GXflIdrlus/видео.html

    • @dantegraphics6259
      @dantegraphics6259 3 года назад +2

      @@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 года назад +1

      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

  • @samkitsanghavi4927
    @samkitsanghavi4927 2 года назад +1

    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.

  • @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

  • @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