1
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?

2
  • 1
    The error you mentioned simply means , .getJSONObject("Root") It is trying to find out a Object with name Root but unable to find or locating another JSON type, Try if you can un-chain the .has part of it in another statement to debug more ! Commented Jul 11, 2022 at 9:57
  • Could you share some info about the library/packages that are you using? Commented Jul 11, 2022 at 10:08

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

yaha..! it's working fine
1

I found the Answer,I am geeting result from SQl server

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

Here JSON Object cannot having values,it only have an Root Tag,So empty json object cannot convert json array,so i've got an exception,After i am changes my result to

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

Thanks

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.