Home > database >  How to parse that JSON to Kotlin Class?
How to parse that JSON to Kotlin Class?

Time:01-04

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:

@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!

CodePudding user response:

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 a single class and then create a typealias for each state and use it instead.

  • Related