0

I'm consuming an API where the data response comes like this :

[{"requester":"5"},{"requester":["5","2"]},{"requester":null}] 

So I'm using retrofit and this is my response model :

public class ResponseModel
 {     private String assigned; 
     public String getRequester()
 {         return requester;  
 }} 

But I didn't know how the response model should be because as you see the same JSON property("requester") can come as a string or as an array if there is only one requester the requester hold only one value as a string but if there is more than one it comes back as an Array and if there is no requester at all it comes back null .

I didn't know how I should do this, hope you can help me with it, appreciate any help!

  • Why not use a try catch? Try to parse it as an array, if that doesn't work try to parse it as a string, and then if that fails it's null. – M. Rogers Nov 04 '22 at 20:32
  • how can I do that? I never used try catch before, I'm new to android appreciate it if you submit an answer and I would try it – Mohamed Aziz Laroussi Nov 04 '22 at 20:50
  • I don't know what retrofit is, but I do know what a JSONObject is. On Android, we use something called a JSONObject to convert Strings to JSONs, which looks like the response your getting (a large String). You're then going to have to take the JSONObject and convert it to a Map of some type. Try this link: https://stackoverflow.com/questions/21720759/convert-a-json-string-to-a-hashmap – BigBeef Nov 05 '22 at 01:25

0 Answers0