![]() |
VOOZH | about |
JSON responses are of 2 types i.e. JSON Object and JSON Array. JSON Array consists of individual JSON objects which are having same similar structure but have different values within it. JSON Array is used to parse the data in the form of a list or an array. In the previous article, we have taken a look at How to extract data from JSON Object in Android. In this article, we will take a look at How to parse Data from JSON array in android using the Retrofit library with Jetpack Compose.
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Adding dependency for using Retrofit
Navigate to app > Gradle Scripts > build.gradle file and add the below dependency to it.
// below dependency for using retrofit. implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
After adding this dependency simply sync your project to install it.
Step 3: Adding a new color in the Color.kt file
Navigate to app > java > your app’s package name > ui.theme > Color.kt file and add the below code to it.
Step 4: Adding permissions to the internet in the AndroidManifest.xml file
Navigate to the app > AndroidManifest.xml and add the below code to it.
Step 5: Creating a Modal class
Navigate to app > java > your app's package name > Right-click on it > New > Kotlin class and specify the name of the class as ListModal and add the below code to it. Comments are added in the code to get to know in detail.
Step 6: Creating an Interface
Navigate to app>java>your app's package name>Right click on it>New>Kotlin class and select interface in it and then specify the name as RetrofitAPI. Add the below code to it. Comments are added in the code to get to know in detail.
Step 7: Working with MainActivity.kt file
Navigate to app>java>your app's package name>MainActivity.kt file and add the below code to it. Comments are added in the code to get to know in detail.
Now run your application to see the output of it.
Output: