I have been trying a method like this but I can't find any solution:
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)
}
I have tried this:
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;
}
But then when I invoke objectToJSONArray(object) I put a JSONObject it crashes casting. So I want a generic method. Someone find any solution?