Java 根据对象将对象转换为 JSONObject 或 JSONArray 的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33055860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:14:19  来源:igfitidea点击:

Method to cast Object to JSONObject or JSONArray depending on the Object

javaandroidjsoncastingjsonobject

提问by Damia Fuentes

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?

但是,当我调用 objectToJSONArray(object) 时,我放了一个 JSONObject,它在投射时崩溃了。所以我想要一个通用的方法。有人找到任何解决方案吗?

采纳答案by Daniel Nugent

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

我假设你已经看到了这个问题。您可以instanceof在从每个方法返回之前添加对 using 类型的检查,如果 Object 不是预期的类型,则返回 null。那应该摆脱 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:

然后,您可以尝试这两种方法,并使用不返回 null 的方法的返回值,如下所示:

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

回答by Pankaj Singhal

JsonArrayis inherently a List

JsonArray本质上是一个列表

JsonObjectis inherently a Map

JsonObject本质上是一个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:

由于 Java 不支持具有多种返回类型的函数(除非它是一个返回类型接受为参数的泛型函数 - example),因此执行您需要的最简单方法如下:

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 JsonObjectinto a JsonArrayand write your code to operate on that JsonArray, without having to worry about the type. The following function serves the said purpose.

如果您希望将其作为函数,另一种方法是将 givenJsonObject转换为 aJsonArray并编写代码以对其进行操作JsonArray,而不必担心类型。以下功能用于上述目的。

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