Lisa Anne
Lisa Anne

Reputation: 4595

Java: is it possible to create a class with "variable" fields?

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 responseand 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 EventsResponsewill contain a response which is JSONObject or a JSONArray depending on the situation.

Please is that possible?

Thanks

Upvotes: 2

Views: 106

Answers (3)

Grim
Grim

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

Amal Gupta
Amal Gupta

Reputation: 452

@LisaAnne. My understanding is given below :-

  1. You have two scenarios out of which, one will return the JsonArray type and other will return JSONObject type.
  2. To ensure that the implementation is clean and written in the same class, you have created two overloaded constructors to fetch the response type from the class which is calling this EventsResponse Class.
  3. The problem however seems to be in the assignment of the responseFromApi (JSONObject or JSONArray depending on the constructor used) to the JSONArray response property, as a direct cast from the Object to JSONArray is not present.

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

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

Related Questions