Reputation: 4595
Thanks for your help.
I have the following situation:
I have a Class that contains the response from an API:
public class EventsResponse extends ApiResponse {
public JSONArray response;
public EventsResponse(Boolean success,RequestHandle requesthandle, JSONArray responsefromapi) {
super(success, requesthandle);
if(responsefromapi!=null)this.response=responsefromapi;
else response=null;
}
public EventsResponse(Boolean success,RequestHandle requesthandle, JSONObject responsefromapi) {
super(success, requesthandle);
if(responsefromapi!=null)this.response=responsefromapi;
else response=null;
}
}
As you can see something is not working here:
if responsefromapi
is a JSONObject
I try to assign it to a response
which is a JSONArray
obviously does not work...
What I would like to do is:
to assign that responsefromapi
to response
and have response
to be a JSONObject
or a JSONArray
depending on what is responsefromapi
.
So that I can use a sigle Class EventsResponse
to handle both situations and EventsResponse
will contain a response
which is JSONObject
or a JSONArray
depending on the situation.
Please is that possible?
Thanks
Upvotes: 2
Views: 106
Reputation: 1976
Try to use Generics:
public class EventsResponse<K> extends ApiResponse {
public K response;
public EventsResponse(Boolean success, RequestHandle requesthandle, K responsefromapi) {
super(success, requesthandle);
response = responsefromapi;
}
}
You can call it like this:
new EventResponse<JSONObject>(...);
AND
new EventResponse<JSONArray>(...);
Upvotes: 3
Reputation: 452
@LisaAnne. My understanding is given below :-
Details :-
As per my understanding, we use JSONObject to store and pass a value of a JSON String something like {name:"LisaAnne", address:"xyz", responseArray:[{name1:"n1"},{name1:"n2"}]}.
JSONObject contains key value pairs Whereas,
JSONArray contains an ordered pair. If you fetch the value of responseArray into a JSONArray type of object, you will be successful.
to fetch an array from the response the following type of statement should work.
JSONArray response= bigDataResponse.getJSONArray("responseArray");
Therefore, a direct cast from the JSONObject to a JSONArray will not be possible. You will have to get the key that contains the array and use the statement given above to fetch it.
I'll be able to give you a better solution if you can give me the response structures received in the JSONObject responsefromapi
and JSONArray responsefromapi
Hope this helps.
Upvotes: 0
Reputation: 27692
Is not very elegant but you could try using Object
common superclass to reference your response:
public Object response;
...
...
public EventsResponse(Boolean success,RequestHandle requesthandle, JSONArray responsefromapi) {
super(success, requesthandle);
if(response!=null)this.response= (Object)responsefromapi;
else response=null;
}
You will need Java's reflection to use the referenced object later in your code:
if(response.getClass().equals(JSONArray.class)) {
...
...
JSONArray responseAsJSONArray = (JSONArray)response;
...
}
Upvotes: 2