简体   繁体   English

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

[英]Method to cast Object to JSONObject or JSONArray depending on the Object

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. 但是,当我调用objectToJSONArray(object)时,我放了一个JSONObject,它使转换崩溃。 So I want a generic method. 所以我想要一个通用方法。 Someone find any solution? 有人找到解决方案吗?

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. 从每个方法返回之前,您可能只需要使用instanceof添加类型检查,如果Object并非预期的类型,则返回null。 That should get rid of the ClassCastException. 那应该摆脱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
    }
}

JsonArray is inherently a List JsonArray本质上是一个列表

JsonObject is 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不支持的功能有多种返回类型(除非它是公认的参数返回类型的通用功能- 例如 ),以执行你所需要的最简单的方法如下:

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. 如果希望将其用作函数,则此方法的替代方法是将给定的JsonObject转换为JsonArray并编写代码以对该JsonArray进行操作,而不必担心类型。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM