Reputation: 115
I am getting some JSON values from the server but I don't know if there will be a particular field or not.I need to validate based on the key .
One Type Of Response
In AysncTask we can use "has" function but in Retrofit i am unable to find a solution .
Provide me a solution
Upvotes: 0
Views: 2426
Reputation: 1192
Retrofit will parse all attributes that you specified in your model. If some attribute in your JSON does not exist Retrofit will set NULL as the value of that attribute.
Knowing this feature, the only thing that you need to do is something like:
if(myObject.getReviewerDetails() == null)
// do something
Happy code!
Upvotes: 3
Reputation: 4705
You can check key exist or not using jsonObject.has like following way,
JSONObject jsonObject=new JSONObject();
if(jsonObject.has("reviewer_details")){
//do process with data
}
Upvotes: 1