Skip to content
Advertisement

Java exception handling: JSONObject cannot convert JSONArray

JavaScript

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

JavaScript

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

JavaScript

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:

JavaScript

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

JavaScript

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