Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to parse that JSON to Kotlin Class?

Server Data:

{
  "active": [
    {
      "title": "ارزیابی آقای غیر حمیدیا",
      "evaluationId": "kjn4a48wd",
      "questionCount": 29,
      "startDate": "876464152132",
      "expireDate": "1651443210"
    },
    {
      "title": "ارزیابی کارکنان تیم فنی",
      "evaluationId": "nbt16541xvf",
      "questionCount": 29,
      "startDate": "876464152132",
      "expireDate": "1651443210"
    },
    {
      "title": "ارزیابی در مورد عماد تقی پور",
      "evaluationId": "kjnfdf1wd",
      "questionCount": 13,
      "startDate": "876464152132",
      "expireDate": "1651443210"
    },
    {
      "title": "ارزیابی چیز دار",
      "evaluationId": "nbtdf65b4vf",
      "startDate": "876464152132",
      "questionCount": 28,
      "expireDate": "1651443210"
    }
  ],
  "abandoned": [
    {
      "title": "ارزیابی آقای حمیدیا",
      "evaluationId": "kjn4a48wd",
      "startDate": "876464152132",
      "questionCount": 17,
      "expireDate": "1651443210"
    },
    {
      "title": "ارزیابی کارکنان تیم فنی",
      "evaluationId": "nbt16541xvf",
      "questionCount": 16,
      "startDate": "876464152132",
      "expireDate": "1651443210"
    },
    {
      "title": "ارزیابی در مورد عماد تقی پور",
      "evaluationId": "kjnfdf1wd",
      "questionCount": 7,
      "startDate": "876464152132",
      "expireDate": "1651443210"
    },
    {
      "title": "ارزیابی کارکنان تیم فنی",
      "evaluationId": "nbtdf65b4vf",
      "questionCount": 2,
      "startDate": "876464152132",
      "expireDate": "1651443210"
    }
  ],
  "expired": [
    {
      "title": "ارزیابی آقای جعفری",
      "evaluationId": "kjn4a48wd",
      "startDate": "876464152132",
      "questionCount": 17,
      "expireDate": "1651443210"
    },
    {
      "title": "ارزیابی کارکنان تیم فنی",
      "evaluationId": "nbt16541xvf",
      "questionCount": 16,
      "startDate": "876464152132",
      "expireDate": "1651443210"
    },
    {
      "title": "ارزیابی در مورد عماد تقی پور",
      "evaluationId": "kjnfdf1wd",
      "questionCount": 7,
      "startDate": "876464152132",
      "expireDate": "1651443210"
    },
    {
      "title": "ارزیابی کارکنان تیم فنی",
      "evaluationId": "nbtdf65b4vf",
      "questionCount": 2,
      "startDate": "876464152132",
      "expireDate": "1651443210"
    },
    {
      "title": "ارزیابی آقای جعفری",
      "evaluationId": "kjn4a48wd",
      "startDate": "876464152132",
      "questionCount": 17,
      "expireDate": "1651443210"
    },
    {
      "title": "ارزیابی کارکنان تیم فنی",
      "evaluationId": "nbt16541xvf",
      "questionCount": 16,
      "startDate": "876464152132",
      "expireDate": "1651443210"
    },
    {
      "title": "ارزیابی در مورد عماد تقی پور",
      "evaluationId": "kjnfdf1wd",
      "questionCount": 7,
      "startDate": "876464152132",
      "expireDate": "1651443210"
    },
    {
      "title": "ارزیابی کارکنان تیم فنی",
      "evaluationId": "nbtdf65b4vf",
      "questionCount": 2,
      "startDate": "876464152132",
      "expireDate": "1651443210"
    }
  ]
}

Model Class:

data class EvaluationListModel(
   var abandoned: List<Abandoned> = ArrayList<Abandoned> (),
   var active: List<Active> = ArrayList<Active> (),
   var expired: List<Expired> = ArrayList<Expired> ())

Retrofit:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@GET("test/first.json")
suspend fun requestEvaluationList(): Response<EvaluationListModel>

My view:

    private fun viewModelObservers() {
    viewModel.serverEvaluationList.observe(viewLifecycleOwner){
        when(it){
            is Resource.Loading->{
                Log.d("evallist", "loading: ")
            }
            is Resource.Success->{
                Log.d("evallist", "succuss: ${it.data?.active?.size} ")
                setEvaluationDataToUi(it.data)
            }
            is Resource.Error->{
                Log.d("evallist", "error: ${it.message} ")
            }
            is Resource.Finish->{
                Log.d("evallist", "finish: ")
            }
            else->{
                Log.d("evallist", "else: ")
            }
        }
}
}

ViewModel:

    @SuppressLint("SuspiciousIndentation")
private fun GetEvaluationListFromServer () = viewModelScope.launch {
    Log.d("evallist", "GetEvaluationListFromServer: ")
    serverEvaluationList.postValue(Resource.Loading())
        try {
            if(hasInternetConnection()){
                val response : Response<EvaluationListModel> = repository.requestGetEvaluationList()
                serverEvaluationList.postValue(handleGetEvaluationListFromServerRequest(response))
            }else{
                serverEvaluationList.postValue(Resource.Error(858585, app.getString(R.string.no_internet) ))
            }
        }catch (e : Exception){
            serverEvaluationList.postValue(e.localizedMessage?.let { Resource.Error(85500, it.toString()) })
        }


}





private fun handleGetEvaluationListFromServerRequest(response: Response<EvaluationListModel>): Resource<EvaluationListModel> {
    return if (response.isSuccessful) {
            serverEvaluationList.postValue(Resource.Success(response.body()))
        Resource.Success(response.body())
    }else if (response.code()==403){
        updateRefreshToken()
        Resource.Error(85403,"چنین کاربری وجود ندارد")
    }else{
        Resource.Error(85600 ,response.message())
    }
}

When Data is parsed size of my lists are zero JSON is valid.

How to parse that JSON to Kotlin Class?

I’m new to Kotlin and when I get the Data in repository and pass it to ViewModel and Observer it in view, it will give list.size = 0

I tried multiple ways of solving this like changing Array List to List etc.

Thank you!

>Solution :

There are plenty of ways to do so, you can do it online, via plugin, manually, etc.

I’ll give you two options.

You can use the json2kt tool where you can choose the package name and the class name.
You can use the JsonToKotlinClass plugin to do so.

I prefer the second one because you can choose not only for Gson but more libraries like Moshi, etc.

So, the response class should be like :

data class EvaluationListModel(
    @SerializedName("abandoned")
    val abandoned: List<Abandoned>,
    @SerializedName("active")
    val active: List<Active>,
    @SerializedName("expired")
    val expired: List<Expired>
)

data class Abandoned(
    @SerializedName("evaluationId")
    val evaluationId: String,
    @SerializedName("expireDate")
    val expireDate: String,
    @SerializedName("questionCount")
    val questionCount: Int,
    @SerializedName("startDate")
    val startDate: String,
    @SerializedName("title")
    val title: String
)

data class Active(
    @SerializedName("evaluationId")
    val evaluationId: String,
    @SerializedName("expireDate")
    val expireDate: String,
    @SerializedName("questionCount")
    val questionCount: Int,
    @SerializedName("startDate")
    val startDate: String,
    @SerializedName("title")
    val title: String
)

data class Expired(
    @SerializedName("evaluationId")
    val evaluationId: String,
    @SerializedName("expireDate")
    val expireDate: String,
    @SerializedName("questionCount")
    val questionCount: Int,
    @SerializedName("startDate")
    val startDate: String,
    @SerializedName("title")
    val title: String
)

Note: Since the body of the Abandoned, Active and Expired is the same you could create one single class and use it instead, but I don’t know if the json is going to change that’s why I’ve split it in three different classes

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading