0

Following is a part of my json array

{
  "format_file_name": [
    "/client/compliance_format/payment_of_bonus_act_1965-ce32ec0ee2b94f819ffd2ffdb95ba439.pdf"
  ]
}

This is the part of my code while Im parsing it,

 JSONArray format_file_name = innerJobj.getJSONArray("format_file_name");
                    if (format_file_name != null) {
                        for (int k = 0; k < format_file_name.length(); k++) {
                            JSONObject jsonObject1 = format_file_name.getJSONObject(i);
                            Iterator<String> keys = jsonObject1.keys();
                            while (keys.hasNext()) {
                                file = jsonObject1.getString(keys.next());

                            }
                        }
                    }

The value of format_file_name might be null some times.

In my code, i have already checked it it is null and in case if it is not null then Im parsing it and assigning it to a string called file

The problem is,

  1. When Im trying to parse the json array of null value, Im getting Value null at format_file_name of type org.json.JSONObject$1 cannot be converted to JSONObject. How can I parse it only when its value is not equal to null?
  2. Why my code doesnt work though I have checked for the condition of null and parsing it only if it is not null?

3 Answers 3

3

you can use isNull method of JSONObject.

if(!innerJobj.isNull("format_file_name"))
{
      //your rest of codes
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try with array.length() method.

JSONArray format_file_name = innerJobj.getJSONArray("format_file_name");
                        if (format_file_name.length() >0) {
                            for (int k = 0; k < format_file_name.length(); k++) {
                                JSONObject jsonObject1 = format_file_name.getJSONObject(i);
                                Iterator<String> keys = jsonObject1.keys();
                                while (keys.hasNext()) {
                                    file = jsonObject1.getString(keys.next());

                                }
                            }
                        }

Comments

0

If you check documentation for getJSONArray method:

Returns the value mapped by name if it exists and is a JSONArray, or throws otherwise.

So if the array is null an exception will be thrown, Instead of using getJSONArray, use another version: optJSONArray

Returns the value mapped by name if it exists and is a JSONArray, or null otherwise.

It returns null without no exception so you can handle it by null check, Try this:

JSONArray format_file_name = innerJobj.optJSONArray("format_file_name");
                    if (format_file_name != null) {
                        for (int k = 0; k < format_file_name.length(); k++) {
                        JSONObject jsonObject1 = format_file_name.optJSONObject(k);
                        ...
                        }
                    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.