Javatpoint Logo
Javatpoint Logo

Kotlin Android JSON Parsing using URL

JSON refers to JavaScript Object Notation, and it is a programming language. JSON is used to parse the data between the server and the client. It is minimal, textual and a subset of JavaScript. It is the alternative to XML parsing.

The advantage of JSON over XML

  1. JSON is faster and easier than XML for AJAX applications.
  2. Unlike XML, it is shorter and quicker to read and write.
  3. It uses an array.

JSON Object

A JSON object contains key/value pairs like a map. The keys are strings and the values are the JSON types. A comma separates keys and values (,). The curly brace {} represents the JSON object.

Kotlin JSON Parsing using URL Example

In this example, we parse the JSON data from URL and bind them into a ListView. The JSON data contains the "id" "name" "email".

JSON data index.html

Create a JSON file index.html.

While executing the JSON file (index.html) looks like as:

Kotlin Android JSON Parsing using URL

activity_main.xml

Add the ListView in the activity_main.xml layout file.

build.gradle

Add the following okhttp dependency in the build.gradle file.

Model.kt

Create a data model class Model.kt which includes the information String "id", String "name" and String "email".

adapter_layout.xml

Create an adapter_layout.xml file in the layout directory which contains the row items for ListView.

CustomAdapter.kt

Create a custom adapter class CustomAdapter.kt and extend BaseAdapter to handle the custom ListView.

MainActivity.kt

Add the following code in MainActivity.kt class file. This class read the JSON data in the form of a JSON object. Using the JSON object, we read the JSON array data. The JSON data are bind in ArrayList.

AndroidManifest.xml

Add the Internet permission in AndroidManifest.xml file.

Output:


Kotlin Android JSON Parsing using URL





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA