skip to Main Content

I have a custom class in Java which extends to JSONArray like the one below

public class JsonArray extends JSONArray {
    
    public void myFunc(){}

}

Now I have a JSONObject that contains JSONArrays in it and I want to cast those JSONArrays to my custom JsonArray class so I can call myFunc() on it

JsonArray array = (JsonArray) jsonObj.getJSONArray(key);
array.myFunc();

But I keep getting an error in my log saying:

JSONArray cannot be cast to JsonArray

Note: I’m running this on Android Studio and this is not my actual code, it’s just a replica, so in this case, we should assume all variables are initialized

3

Answers


  1. Chosen as BEST ANSWER

    Referencing to @StephenC's... I decided to use his second suggestion.

    So adding a constructor to my JsonArray class that will take in an argument of a JSONArray, then I add all elements in the JSONArray to JsonArray

    public JsonArray(JSONArray array) throws JSONException {
        for(int i = 0; i < array.length(); i++){
            String s = array.getString(i);
            this.put(i, s);
        }
    }
    

    Usage:

    JsonArray array = new JsonArray(jsonObj.getJSONArray(key));
    

  2. What’s Happening

    Here you will create JsonArray.java class and extend it with JSONArray.
    it mean your JsonArray is child of parent class JSONArray.

    Now you have wrote code like.

    JsonArray array = (JsonArray) jsonObj.getJSONArray(key);
    

    JsonArray is a child, and jsonObj.getJSONArray(key); is a parent. that was problem.

    Solution:-

    Cast it by child class like.

    JsonArray array = (JsonArray) new JsonArray();
    
    Login or Signup to reply.
  3. Basically, what you are trying to do won’t work.

    The object returned by jsonObj.getJSONArray(key) is clearly a JSONArray and not a JsonArray. And no type cast is going to change the actual type of an object. That is just not possible in Java. A reference type to reference type cast in Java does not change the actual class of the object.

    If you want to arrange that you can use the result of jsonObj.getJSONArray(key) as your custom JsonArray type, you are going to have to create it with that type in the first place.

    How?

    • Possibly using a JSON binding with a custom object mapper. (But I have my doubts that approach will work.)

    • Possibly by making your JsonArray a wrapper class for JSONArray. And then do this to wrap the JSONArray instance that you get; e.g.

      JsonArray array = new JsonArray(jsonObj.getJSONArray(key));
      
    • Possibly some other way.

    The best way will depend on the actual JSON library you are using and how you are instantiating the JSON classes … like jsonObject in your example.

    But it is probably simpler if you don’t try to subclass JSONArray like this. IMO, it is only making things difficult. Instead, just put the myFunc functionality into a domain class or a utility class or something.


    UPDATE – If I was going to use the wrapper approach, I would do it like this:

    public class JsonArray extends JSONArray {
    
        private JSONArray ja;
        
    
        public JsonArray(JSONArray ja) {
            this.ja = ja;
        }
    
        public void myFunc(){
            // whatever
        }
    
        // Wrapper implementations for all of the JSONArray methods
        @Override
        public java.lang.Object get(int index) {
            return ja.get(index);
        }
    
        // etcetera
    }
    

    You don’t necessarily need to write all of that by hand. Some IDEs can automatically generate a skeleton for a wrapper class from the class that you are trying to wrap.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search