0

My Json Object :

 "feedback_details":{
"id":"80",
"lead_id":"86075",
"account_id":"1543",
"document_id":"582348",
"reason_id":"4",
"description":"COAPPL_profile1.jpg",
"sales_remark":null,
"lat":"",
"long":"",
"status":"2",
"created_at":"2018-02-27 12:29:59",
"updated_at":"2018-02-27 12:29:59"
}

When i have no details in feedback_details then it comes to an empty json array like this :

"feedback_details":[
]

So i tried converting it forcefully to a blank json object :

for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject object = jsonArray.getJSONObject(i);

            for (int j = 0; j < object.getJSONArray("applicantPhoto").length(); j++) {
                object.getJSONArray("applicantPhoto").getJSONObject(j);
                if (object.getJSONArray("applicantPhoto").getJSONObject(j).optJSONObject("feedback_details") == null) {
                    new JSONObject(object.getJSONArray("applicantPhoto").getJSONObject(j).getString("feedback_details"));
                }
                if (object.getJSONArray("applicantSignatire").getJSONObject(j).optJSONObject("feedback_details") == null) {
                    String newstring = object.getJSONArray("applicantSignatire").getJSONObject(j).getJSONArray("feedback_details").toString();
                    //here i get the exception for type mismatch where the string is "[]"
                    new JSONObject(newstring);
                }
                if (object.getJSONArray("applicantPhoto").getJSONObject(j).optJSONObject("feedback_details") == null) {
                    new JSONObject(String.valueOf(object.getJSONArray("applicantPhoto").getJSONObject(j).getJSONArray("feedback_details")));
                }
            }
            Utility.printMessage("Json object..." + object);

I know that string is "[]", hence it showing an exception as Cannot be converted to JsonObject. Can anyone suggest how to handle this, basically i need an empty object instead of that blank array.

Siddhesh Pawar
  • 259
  • 4
  • 24
  • You can check solution. https://stackoverflow.com/questions/6118708/determine-whether-json-is-a-jsonobject-or-jsonarray – Shohel Rana Mar 05 '18 at 07:02

1 Answers1

0

Please check the below code

if(!object.getJSONArray("applicantPhoto").getJSONObject(j).isNull("feedback_details")) {
        if (object.getJSONArray("applicantPhoto").getJSONObject(j).getJSONObject("feedback_details").toString().length() > 2) {
            for (int i = 0; i < object.getJSONArray("applicantPhoto").getJSONObject(j).getJSONObject("feedback_details").length(); i++) {
                JSONObject jsonObject1 = object.getJSONArray("applicantPhoto").getJSONObject(j).getJSONObject("feedback_details").getJSONObject(i);
                //Get the Values of Feedback_details Key
            }
        } else {
            //Feedback_detail key is empty
        }
    } else {
        //Key is null or Array is empty
    }
Saurabh Vadhva
  • 511
  • 5
  • 12