Methode to cast Object zu JSONObject oder JSONArray je nach Objekt

Habe ich versucht, eine Methode wie diese, aber ich finde keine Lösung:

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)
}

Ich habe dies ausprobiert:

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;
}

Aber wenn ich dann aufrufen objectToJSONArray(Objekt -) ich habe ein JSONObject es kracht, Gießen. Also möchte ich eine generische Methode. Jemanden zu finden, keine Lösung?

Poste bitte Logcat.

InformationsquelleAutor Damia Fuentes | 2015-10-10

Schreibe einen Kommentar