6

How to parse JSON in Android using Kotlin

 3 years ago
source link: https://johncodeos.com/how-to-parse-json-in-android-using-kotlin/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to parse JSON in Android using Kotlin

April 12, 2021April 12, 2021 by John Codeos

We’re going to parse JSON without using any 3rd party library, but using the java class JSONTokener.

With the JSONTokener, we can parse a JSON string into an object. These objects can be cast as JSONObject or as JSONArray. In that way, we’ll able to read the JSON values.

I’ll cover 3 cases that may you face when you’re trying to parse JSON:

  • Simple JSON: Just a simple JSON without any complex structure
  • Array JSON: The JSON structure begins with an array and without a key
  • Nested JSON: Includes nested objects

Simple JSON

A simple JSON doesn’t have any complex structure, like arrays, nested objects, e.t.c, and it looks like that:

{ "id": "1", "employee_name": "Jack Full", "employee_salary": "300800", "employee_age": "61" }

Code language: Swift (swift)

After getting the response from the HTTP request, you parse the JSON string into JSONObject, to get the JSON values:

val jsonObject = JSONTokener(response).nextValue() as JSONObject // ID val id = jsonObject.getString("id") Log.i("ID: ", id) // Employee Name val employeeName = jsonObject.getString("employee_name") Log.i("Employee Name: ", employeeName) // Employee Salary val employeeSalary = jsonObject.getString("employee_salary") Log.i("Employee Salary: ", employeeSalary) // Employee Age val employeeAge = jsonObject.getString("employee_age") Log.i("Employee Age: ", employeeAge)

Code language: Kotlin (kotlin)

Array JSON

Sometimes, you might see a JSON start with an array and without having a key:

[ { "id": "1", "employee_name": "Tiger Nixon", "employee_salary": "320800", "employee_age": "61" }, { "id": "2", "employee_name": "Garrett Winters", "employee_salary": "170750", "employee_age": "63" }, // ... ]

Code language: Swift (swift)

After getting the response, you parse the JSON string into a JSONArray and you’re looping through the array to get the JSON values.

val jsonArray = JSONTokener(response).nextValue() as JSONArray for (i in 0 until jsonArray.length()) { // ID val id = jsonArray.getJSONObject(i).getString("id") Log.i("ID: ", id) // Employee Name val employeeName = jsonArray.getJSONObject(i).getString("employee_name") Log.i("Employee Name: ", employeeName) // Employee Salary val employeeSalary = jsonArray.getJSONObject(i).getString("employee_salary") Log.i("Employee Salary: ", employeeSalary) // Employee Age val employeeAge = jsonArray.getJSONObject(i).getString("employee_age") Log.i("Employee Age: ", employeeAge) // Save data using your Model // Notify the adapter } // Pass adapter to the RecyclerView adapter

Code language: Kotlin (kotlin)

Tip: If a key does not exist in some objects, you can set it as optional like this:
val id = jsonArray.getJSONObject(i).optString(“id”)

Nested JSON

When a JSON object is inside another JSON object, it’s called ‘nested’, and will look like the following JSON structure:

{ "data": [ { "id": "1", "employee": { "name": "Tiger Nixon", "salary": { "usd": 320800, "eur": 273545 }, "age": "61" } }, { "id": "2", "employee": { "name": "Garrett Winters", "salary": { "usd": 170750, "eur": 145598 }, "age": "63" } }, // ... ] }

Code language: Swift (swift)

After you get the response, you parse the JSON string into a JSONObject, then you get the “data” as a JSONArray, and you loop through the array.

val jsonObject = JSONTokener(response).nextValue() as JSONObject val jsonArray = jsonObject.getJSONArray("data") for (i in 0 until jsonArray.length()) { // ID val id = jsonArray.getJSONObject(i).getString("id") Log.i("ID: ", id) // Employee val employee = jsonArray.getJSONObject(i).getJSONObject("employee") // Employee Name val employeeName = employee.getString("name") Log.i("Employee Name: ", employeeName) // Employee Salary val employeeSalary = employee.getJSONObject("salary") // Employee Salary in USD val employeeSalaryUSD = employeeSalary.getInt("usd") Log.i("Employee Salary in USD: ", employeeSalaryUSD.toString()) // Employee Salary in EUR val employeeSalaryEUR = employeeSalary.getInt("eur") Log.i("Employee Salary: ", employeeSalaryEUR.toString()) // Employee Age val employeeAge = employee.getString("age") Log.i("Employee Age: ", employeeAge) // Save data using your Model // Notify the adapter } // Pass adapter to the RecyclerView adapter

Code language: Kotlin (kotlin)

You can find the final project here

If you have any questionsplease feel free to leave a comment below


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK