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?
.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 canun-chainthe .has part of it in another statement to debug more !