title: Method to cast Object to JSONObject or JSONArray depending on the Object
body: <p>I have been trying a method like this but I can't find any solution:</p>
  
  <pre><code>public static JSONObject or JSONArray objectToJSON(Object object){
      if(object is a JSONObject)
          return new JSONObject(object)
      if(object is a JSONArray)
          return new JSONArray(object)
  }
  </code></pre>
  
  <p>I have tried this:</p>
  
  <pre><code>public static JSONObject objectToJSONObject(Object object){
      Object json = null;
      try {
          json = new JSONTokener(object.toString()).nextValue();
      } catch (JSONException e) {
          e.printStackTrace();
      }
      JSONObject jsonObject = (JSONObject)json;
      return jsonObject;
  }
  
  public static JSONArray objectToJSONArray(Object object){
      Object json = null;
      try {
          json = new JSONTokener(object.toString()).nextValue();
      } catch (JSONException e) {
          e.printStackTrace();
      }
      JSONArray jsonObject = (JSONArray)json;
      return jsonObject;
  }
  </code></pre>
  
  <p>But then when I invoke objectToJSONArray(object) I put a JSONObject it crashes casting. So I want a generic method. Someone find any solution?</p>