Gokul Rajkumar
Gokul Rajkumar

Reputation: 115

How to check a particular key exists in JSON response using Retrofit Library

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

enter image description here

Another Type Of Responseenter image description here

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

Answers (2)

Robert
Robert

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

Dhaval Solanki
Dhaval Solanki

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

Related Questions