How to Parse JSON in Kotlin

How to parse JSON in Kotlin?

You can use this library https://github.com/cbeust/klaxon

Klaxon is a lightweight library to parse JSON in Kotlin.

How to Parse Json in Kotlin Using Retrofit?

Status, message and data are all part of the response so you need to take care of that. For example this

data class AddUserResponse(
val `data`: UserInfo, //like you defined it
val message: String,
val status: Int,
val time: String
)

This means parameter and response are different so the RestApi needs to be changed to this

abstract fun addUser(@Body userData: UserInfo): Call<AddUserResponse>}

This in turn also change the types in the service like

class RestApiService
{
fun addUser(userData: UserInfo, onResult: (UserInfo?) -> Unit)
{
val retrofit = ServiceBuilder.buildService(RestApi::class.java)
retrofit.addUser(userData).enqueue(
object : Callback<AddUserResponse>
{
override fun onFailure(call: Call<AddUserResponse>, t: Throwable)
{
onResult(null)
}

override fun onResponse( call: Call<AddUserResponse>, response: Response<AddUserResponse>)
{
val addedUser = response.body()
Log.d("responsee",""+addedUser)
onResult(addedUser.data)
}
}
)
}
}

now in getQuotes you will have that it is a UserInfo object

    apiService.addUser(userInfo) {
val returnedUserInfo = it
}

How to parse JSON data through Retrofit, Coroutine with Kotlin in android studio

I think that you schould use "/users" with small "u":

interface ApiInterface {
@GET("/users")
suspend fun getUsers(): Response<Users>
}

insted of:

interface ApiInterface {
@GET("/Users")
suspend fun getUsers(): Response<Users>
}

Kotlin Gson parsing Json Object and Array

First you need to define two types that map to your JSON structure

class ApiParameterData(
val apiKey: String,
val baseUrl: String,
val requestData: List<RequestObject>
)

class RequestObject(
val lng: String,
val lat: String,
val rad: String,
val type: List<String>
)

Now simply parse it as

val apiData = Gson().fromJson(readJson, ApiParameterData::class.java)     // No need to add TypeAdapter

// To get requestData
val requestData = apiData.requestData
requestData.forEach {
print("${it.lng}, ${it.lat}, ${it.rad}, ${it.type})
}

Using Kotlin to parse nested json array/object

You must first get JSONArray from the object according to the following code and then access the class_name variable

val obj = JSONObject(js)
val jsonArray = obj.getJSONArray("childScoresList")
for (i in 0 until jsonArray.length()) {
val classes = jsonArray.getJSONObject(i).getJSONArray("classes")
for (x in 0 until classes.length()) {
val categoryName = classes.getJSONObject(x).getString("category_name")
val className = classes.getJSONObject(x).getString("class_name")
}
}

Kotlin parsing JSON

It didn't solve the JSON issue, but i got my information by changing the string to

info1|info2|info3|etc

then on kotlin side

val lstValues: List<String> = response.split("|").map { it -> it.trim() }

var fname = lstValues.elementAt(0)
var lname = lstValues.elementAt(1)
Toast.makeText(applicationContext, "Welcome "+fname + " "+lname, Toast.LENGTH_LONG).show()


Related Topics



Leave a reply



Submit