how to check if a JSONArray is empty in java?

前端 未结 4 907
温柔的废话
温柔的废话 2021-01-01 11:28

I am working on an android app that get json content of a webservice called \"WebUntis\". The Json content i am getting looks like:

{\"jsonrpc\":\"2.0\",\"id         


        
4条回答
  •  失恋的感觉
    2021-01-01 12:16

    This is another way to do it...

    call.enqueue(new Callback() {
        @Override
        public void onResponse(Call call,
                               Response response) {
    
            JSONObject jsonObject;
            try {
                jsonObject = new JSONObject(new Gson().toJson(response.body()));
    
    
                JSONArray returnArray = jsonObject.getJSONArray("my_array");
    
                // Do Work Only If Not Null
                if (!returnArray.isNull(0)) {
    
                    for (int l = 0; l < returnArray.length(); l++) {
                        if (returnArray.length() > 0) {
    
                            // Get The Json Object
                            JSONObject returnJSONObject = returnArray.getJSONObject(l);
    
                            // Get Details
                            String imageUrl = returnJSONObject.optString("image");
    
                        }
                    }
                }
    
            } catch (JSONException e) {
                e.printStackTrace();
            }
    
        }
    
        @Override
        public void onFailure(Call call,
                              Throwable t) {
    
        }
    });
    

提交回复
热议问题