Android volley : 如何在解析前检查 json 类型

Android volley : how to check json type before parsing it

是否可以事先检查json的类型?我有时会得到一个数组,有时还会得到一个对象,但我不知道如何在不执行 2 个不同函数的情况下处理这 2 种情况...

public void RequestApi( String url, final ApiResponse<ApiResult> completion  )
    {
        Log.v("Performing request: ", url);
        JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, hostname+url, (JSONObject) null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response)
                {
                    Log.v("RequestApi Response", response.toString());
                    //Log.v("Data: ", response.toString());
                    try {
                        ApiResult res = new ApiResult();
                        Boolean success = response.getBoolean("success");

                        //here need to check type, sometimes array, sometimes object
                        JSONArray data = response.getJSONArray("data"); 

                        res.success = success;
                        res.data = data;
                        completion.onCompletion(res);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    ApiResult res = new ApiResult();
                    res.success = false;
                    completion.onCompletion(res);
                }
            }
        );

        AppController.getInstance().addToRequestQueue(jsonRequest);
    }

最简单的解决方案 - 查看第一个字符串字符。如果是 { - 对象,如果 [ - 数组。但我认为最好做以下事情:

try {
    JSONObject object = new JSONObject(value);
}
catch (JSONException) {
    //if it throws, "value" contains not a JSONObject
    JSONArray array = new JSONArray(value);
}

使用StringRequest

  // Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
       Object json = new JSONTokener(response).nextValue();
       if (json instanceof JSONObject)
         //you have an object
       else if (json instanceof JSONArray)
       //you have an array
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        // TODO something
    }
});

经过 API 我找到了一个简单的解决方案。 由于您不确定 return 类型,请按照以下步骤操作。

第一个:

JSONArray arrayInstance = new JSONArray();// declare array instance to handle both the cases 
Object objectInstance = rootObject.get(key); //key is the response string holding the value either jsonArray or jsonObject

第二个:

if(objectInstance instanceof JSONArray){
       arrayInstance = rootObject.getJSONArray(key);
    }

else{
        JSONObject tempObject = rootObject.getJSONObject(key);
        arrayInstance.put(tempObject);
    }

第三: 您可以遍历数组以进行所需的处理。