Skip to content
Advertisement

Java exception handling: JSONObject cannot convert JSONArray

private String getExpenseDtl() throws Exception {
    CommonDO commonDO = new CommonDO(this.ou);
    HttpServletRequest request = ServletActionContext.getRequest();
    try {
        String getData = '<Root></Root>' //Sql returned xml result
        if (!getData.equals("~") && !getData.equals("") && !getData.equals("<Root/>")) {
            JSONObject xmlJSONObj = XML.toJSONObject(getData);
            JSONArray headerFormat = null;
            if (xmlJSONObj.has("Root")) {
                if (xmlJSONObj.getJSONObject("Root").has("AmountDtl"))   //Here i am getting error
                { 
                    Object jsonObjData = xmlJSONObj.getJSONObject("Root").get("AmountDtl");
                    if (!jsonObjData.toString().contains("[")) {
                        String jsonStrData = "[" + jsonObjData.toString() + "]";
                        headerFormat = new JSONArray(jsonStrData);
                    } else {
                        headerFormat = (JSONArray) xmlJSONObj.getJSONObject("Root").get("AmountDtl");
                    }
                }
            }
            request.setAttribute("amountDetailsData", headerFormat);
        } else {
            getData = "~";
            request.setAttribute("amountDetailsData", getData);

        }
    } catch (Exception e) {
        getData = "~";
        request.setAttribute("amountDetailsData", getData);
        System.out.println("[" + new Date().toString() + "]" + e.getMessage());
        String errMsg = e.toString().replaceAll("[^\w\s]", "");
        commonDO.insertLogDetails(errMsg);
    }
    return "expense";
}

After executeQuery, I’ve got some set of results in XML format. While checking xmlJSONObj.getJSONObject("Root").has("AmountDtl"), I get an exception “JSONObject[“Root”] is not a JSONObject”. How to handle this exception?

Advertisement

Answer

In your case.

You have

String getData = '<Root></Root>';

If you convert it to JSONObject you get something like this.

{
  "Root": {},
}

based on what you are saying you are getting an exception here:

if (xmlJSONObj.getJSONObject(“Root”).has(“AmountDtl”))

getJSONObject is trying to get a JSONObject nested inside a JSONObject an example of a useful situation for this is for example if you have a JSONObject like the following one:

{"isbn": "123-456-222",  
 "author": 
    {
      "lastname": "Doe",
      "firstname": "Jane"
    },
"editor": 
    {
      "lastname": "Smith",
      "firstname": "Jane"
    },
  "title": "The Ultimate Database Study Guide",  
  "category": ["Non-Fiction", "Technology"]
 }

In this example you could have a getJSONObject(“author”) and get the JSONObject:

{         "lastname": "Doe",
          "firstname": "Jane"
        }

here some info about it: https://www.ibm.com/docs/no/db2/11.5?topic=documents-json-nested-objects

Could you post a example of your variable getData? in the example you have posted it is not clear but i would say that your problem is that the way your JSONObject is being created doesn´t create nested JSONObjects

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement