Reputation: 797
The JSON response is :
{
"success": false,
"errorMessages": [
"You have to select a maximum load of <span style='color:red;'>0</span> Credit/Course but you have selected <span style='color:red;'>3</span> Credit/Course --- [R060]",
"You can register courses as a full study with a load limit between <span style='color:red;'>12</span> and <span style='color:red;'>18</span> Credit/Course, but you have selected <span style='color:red;'>9</span> Credit/Course --- [R062]"
],
"isConflict": 0
}
but when isConflict == 1
the response is:
{
"ignoreConflictValue": "W",
"isConflict": 1,
"conflict": [
{
"EXAM_DATE": "01/01/2019",
"START_TIME": "08:00 AM",
"END_TIME": "09:30 AM",
"COURSE_NAME_SL": "مقاومة مواد,تقنيات الحفر البحري",
"COURSE_NAME_PL": "STRENGTH OF MATERIALS,OFFSHORE TECHNOLOGY",
"COURSES_COUNT": "2",
"ACTIVE": "A"
}
],
"success": false
}
The logic of this API is :
isConflict == 1
the success property is type of Integer
with values 1 and 0.Boolean
with values true
and false
My question is how to define the POJO class for this situation.
I tried to make two fields with the same name with @Nullable
for both but Gson
complains that the POJO has duplicate fields.
Upvotes: 0
Views: 1233
Reputation: 8713
In Kotlin you can do the following:
Define "ApiResponse" class with generics like below:
class ApiResponse(@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 is Boolean){
Log.v(TAG,"Boolean")
} else{
Log.v(TAG,"not boolean")
}
Upvotes: 1