Skip to content Skip to sidebar Skip to footer

Handle Json Response With Multiple Type Of The Same Name

The JSON response is : { 'success': false, 'errorMessages': [ 'You have to select a maximum load of 0 Credit/Course but

Solution 1:

In Kotlin you can do the following:

Define "ApiResponse" class with generics like below:

classApiResponse(@SerializedName("success") val success : Any,
                  @SerializedName("errorMessages") val errorMessages : Array<Any>,
                  @SerializedName("isConflict")
                  val isConflict : Integer)

Then, in your activity, use Gson to convert the response with

var responseOne = Gson().fromJson(textConflictOneResponse, ApiResponse::class.java)
        var responseZero = Gson().fromJson(textConflictZeroResponse, ApiResponse::class.java)

Then you can check the type of the response by doing:

if (responseOne.success isBoolean){
            Log.v(TAG,"Boolean")
        } else{
            Log.v(TAG,"not boolean")
        }

Post a Comment for "Handle Json Response With Multiple Type Of The Same Name"