Kotlin에서 외부 라이브러리 사용하지 않고 JSON 데이터 파싱하는 3가지 방법
How to parse JSON in Android using Kotlin | John Codeos - Blog with Free iOS & Android Development Tutorials Parse JSON (simple, array, and nested) in Android without using any 3rd party library johncodeos.com 심플한 JSON, 배열로 된 JSON, 그리고 중첩된 JSON에 데이터에 대해 파싱하는 방법을 설명합니다. 심플한 JSON JSON 예: { "id": "1", "employee_name": "Jack Full", "employee_salary": "300800", "employee_age": "61" } HTTP를 통해 response로 데이터를 받았다고 가정할 때 JSON 문자열을 JSONObject로 파싱한 후 데이터를 추출합니다. val jsonObject = JSONTokener(response).nextValue() as JSONObject // ID val id = jsonObject.getString("id") Log.i("ID: ", id) // Employee 이름 val employeeName = jsonObject.getString("employee_name") Log.i("Employee Name: ", employeeName) // Employee 급여 val employeeSalary = jsonObject.getString("employee_salary") Log.i("Employee Salary: ", employeeSalary) // Employee 나이 val employeeAge = jsonObject.getString("employee_age") Log.i("Employee Age: ", employeeAge) 배열로 된 JSON JSON 예: [ { "id": "1", "employee_name": "Tiger Nixon", "employee_salary": "320800", "employee_age": "61" }, { "id": "2", "employee_name": "Garrett Winters", "employee_salary": "170750", "employee_age": "63" }, // ... ] JSON 문자열을 JSONArray로 파싱한 후 반복 루프를 돌면서 값을 추출합니다. 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 이름 val employeeName = jsonArray.getJSONObject(i).getString("employee_name") Log.i("Employee Name: ", employeeName) // Employee 급여 val employeeSalary = jsonArray.getJSONObject(i).getString("employee_salary") Log.i("Employee Salary: ", employeeSalary) // Employee 나이 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 중첩된 JSON JSON 객체 안에 또 다른 JSON 객체가 있는 경우 중첩되었다고 합니다. JSON 예 : { "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" } }, // ... ] } JSON 문자열을 JSONObject 로 파싱한 다음 JSON 데이터의 키값(위 예에서는 'data')으로 JSONArray을 가져와서 루프를 돌며 값을 추출합니다. 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 이름 val employeeName = employee.getString("name") Log.i("Employee Name: ", employeeName) // Employee 급여 val employeeSalary = employee.getJSONObject("salary") // Employee 급여(달러화) val employeeSalaryUSD = employeeSalary.getInt("usd") Log.i("Employee Salary in USD: ", employeeSalaryUSD.toString()) // Employee 급여(유로화) 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