6

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?

Damia Fuentes
  • 5,308
  • 6
  • 33
  • 65

2 Answers2

11

I assume you've seen this question. You can probably just add a check of the type using instanceof before you return from each method, and return null if the Object is not of the type expected. That should get rid of the ClassCastException.

Example:

public static JSONObject objectToJSONObject(Object object){
    Object json = null;
    JSONObject jsonObject = null;
    try {
        json = new JSONTokener(object.toString()).nextValue();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    if (json instanceof JSONObject) {
        jsonObject = (JSONObject) json;
    }
    return jsonObject;
}

public static JSONArray objectToJSONArray(Object object){
    Object json = null;
    JSONArray jsonArray = null;
    try {
        json = new JSONTokener(object.toString()).nextValue();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    if (json instanceof JSONArray) {
        jsonArray = (JSONArray) json;
    }
    return jsonArray;
}

Then, you can try both methods, and use the return value of the one that doesn't return null, something like this:

public void processJSON(Object obj){
    JSONObject jsonObj = null;
    JSONArray jsonArr = null;
    jsonObj = objectToJSONObject(obj);
    jsonArr = objectToJSONArray(obj);
    if (jsonObj != null) {
        //process JSONObject
    } else if (jsonArr != null) {
        //process JSONArray
    }
}
Community
  • 1
  • 1
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
0

JsonArray is inherently a List

JsonObject is inherently a Map

Since Java doesn't support functions having multiple return types(unless it is a generic function with return type accepted as argument - example), the most simple way to perform what you need is the following:

if (object instanceof Map){
    JSONObject jsonObject = new JSONObject();
    jsonObject.putAll((Map)object);
    ...
    ...
}
else if (object instanceof List){
    JSONArray jsonArray = new JSONArray();
    jsonArray.addAll((List)object);
    ...
    ...
}

An alternative to this, if you want it as a function, is to convert the given JsonObject into a JsonArray and write your code to operate on that JsonArray, without having to worry about the type. The following function serves the said purpose.

public JSONArray getJsonObjectOrJsonArray(Object object){
    JSONArray jsonArray = new JSONArray();
    if (object instanceof Map){
        JSONObject jsonObject = new JSONObject();
        jsonObject.putAll((Map)object);
        jsonArray.add(jsonObject);
    }
    else if (object instanceof List){
        jsonArray.addAll((List)object);
    }
    return jsonArray;
}
Community
  • 1
  • 1
Pankaj Singhal
  • 15,283
  • 9
  • 47
  • 86