Android JSON Parsing Kotlin

In this tutorials we will see how to parse JSON in kotlin , here we will keep json file inside assets folder of the project , read then content of json file and then parse to read individual json field .

Also Read : Reading File From Android Asset Folder 


1. Reading Simple JSON


file : app/src/main/assets/sample.json
{
  "name": "John",
  "age": 29,
  "height": 5.8
}

Reading Above JSON file from asset folder and Parsing .

    private fun readSampleJSON(): Unit {
        val file_name = "sample.json"
        val bufferReader = application.assets.open(file_name).bufferedReader()
        val data = bufferReader.use {
            it.readText()
        }

        val jsonObject = JSONObject(data)

        val name = jsonObject.getString("name")
        val age = jsonObject.getInt("age")
        val height = jsonObject.getDouble("height")

        Log.d("readSampleJSON", "name : $name || age : $age || height: $height")
    }

 Logcat: 

2019-08-31 13:35:12.101 20974-20974/? D/readSampleJSON: name : John || age : 29 || height: 5.8


2. Reading ArrayOf JSONObject


file : app/src/main/assets/android_version.json
[
  {
    "name": "Cupcake",
    "version": "Android 1.5"
  },
  {
    "name": "Donut",
    "version": "Android 1.6"
  },
  {
    "name": "Eclairs",
    "version": "Android 2.0-2.1"
  },
  {
    "name": "Froyo",
    "version": "Android 2.2-2.3"
  },
  {
    "name": "Gingerbread",
    "version": "Android 2.3-2.3.7"
  },
  {
    "name": "Honeycomb",
    "version": "Android 3.0-3.2.6"
  },
  {
    "name": "Icecream",
    "version": "Android 4.0-4.0.4"
  },
  {
    "name": "Jellybean",
    "version": "Android 4.1-4.3.1"
  },
  {
    "name": "Kitkat",
    "version": "Android 4.4-4.4.4"
  },
  {
    "name": "Lolipop",
    "version": "Android 5.0-5.1.1"
  },
  {
    "name": "Marshmallow",
    "version": "Android 6.0-6.0.1"
  },
  {
    "name": "Nougat",
    "version": "Android 7.0-7.1.2"
  },
  {
    "name": "Oreo",
    "version": "Android 8.0-8.1"
  },
  {
    "name": "Pie",
    "version": "Android 9.0"
  }
]

Reading Above JSON File From Asset Folder and Parsing .

    private fun readArrayOfJsonObject(): Unit {

        val bufferReader = application.assets.open("android_version.json").bufferedReader()
        val json_string = bufferReader.use {
            it.readText()
        }
        val jsonArray = JSONArray(json_string);

        for (i in 0..jsonArray.length() - 1) {
            val jsonObject: JSONObject = jsonArray.getJSONObject(i)

            val name = jsonObject.getString("name")
            val version = jsonObject.getString("version")

            Log.d("readArrayOfJsonObject", "name: $name || version : $version  \n")
        }
    }

Log-cat:
2019-08-31 13:35:12.104 20974-20974/? D/readArrayOfJsonObject: name: Cupcake || version : Android 1.5
2019-08-31 13:35:12.104 20974-20974/? D/readArrayOfJsonObject: name: Donut || version : Android 1.6
2019-08-31 13:35:12.104 20974-20974/? D/readArrayOfJsonObject: name: Eclairs || version : Android 2.0-2.1
2019-08-31 13:35:12.104 20974-20974/? D/readArrayOfJsonObject: name: Froyo || version : Android 2.2-2.3
2019-08-31 13:35:12.104 20974-20974/? D/readArrayOfJsonObject: name: Gingerbread || version : Android 2.3-2.3.7
2019-08-31 13:35:12.104 20974-20974/? D/readArrayOfJsonObject: name: Honeycomb || version : Android 3.0-3.2.6
2019-08-31 13:35:12.104 20974-20974/? D/readArrayOfJsonObject: name: Icecream || version : Android 4.0-4.0.4
2019-08-31 13:35:12.104 20974-20974/? D/readArrayOfJsonObject: name: Jellybean || version : Android 4.1-4.3.1
2019-08-31 13:35:12.104 20974-20974/? D/readArrayOfJsonObject: name: Kitkat || version : Android 4.4-4.4.4
2019-08-31 13:35:12.105 20974-20974/? D/readArrayOfJsonObject: name: Lolipop || version : Android 5.0-5.1.1
2019-08-31 13:35:12.105 20974-20974/? D/readArrayOfJsonObject: name: Marshmallow || version : Android 6.0-6.0.1
2019-08-31 13:35:12.105 20974-20974/? D/readArrayOfJsonObject: name: Nougat || version : Android 7.0-7.1.2
2019-08-31 13:35:12.105 20974-20974/? D/readArrayOfJsonObject: name: Oreo || version : Android 8.0-8.1
2019-08-31 13:35:12.105 20974-20974/? D/readArrayOfJsonObject: name: Pie || version : Android 9.0  

Full Code

3. Activity


file : MainActivity.kt
package com.tutorialsbuzz.androidjsonparse

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import org.json.JSONArray
import org.json.JSONObject

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        readSampleJSON()

        readArrayOfJsonObject()

    }


    private fun readSampleJSON(): Unit {
        val file_name = "sample.json"
        val bufferReader = application.assets.open(file_name).bufferedReader()
        val data = bufferReader.use {
            it.readText()
        }

        val jsonObject = JSONObject(data)

        val name = jsonObject.getString("name")
        val age = jsonObject.getInt("age")
        val height = jsonObject.getDouble("height")

        Log.d("readSampleJSON", "name : $name || age : $age || height: $height")
    }


    private fun readArrayOfJsonObject(): Unit {

        val bufferReader = application.assets.open("android_version.json").bufferedReader()
        val json_string = bufferReader.use {
            it.readText()
        }
        val jsonArray = JSONArray(json_string);

        for (i in 0..jsonArray.length() - 1) {
            val jsonObject: JSONObject = jsonArray.getJSONObject(i)

            val name = jsonObject.getString("name")
            val version = jsonObject.getString("version")

            Log.d("readArrayOfJsonObject", "name: $name || version : $version  \n")
        }
    }
}


No comments:

Post a comment