IAmYourFaja
IAmYourFaja

Reputation: 56912

Java JSON library throwing exception

I have the following JSON string:

{"widget":{"order":{"fizz":["23", "55"],"setting":"always"}},"resp":"ok"}

And the following Java code:

try {
    JSONObject json = getTheAboveStringAsJSON();
    order = json.getJSONObject("order");
} catch(JSONException e) {
    throw new RuntimeExcept(e.getMessage());
}

Giving me the following exception:

JSONObject["order"] not found.
java.lang.RuntimeException: JSONObject["order"] not found.

What's going on here? Why the exception? What can I do to fix it? Thanks in advance!

Upvotes: 0

Views: 256

Answers (2)

Ruan Mendes
Ruan Mendes

Reputation: 92274

Your structure is

{
    "widget":{
        "order":{
            "fizz":["23", "55"],
            "setting":"always"
         }
     },
     "resp":"ok"
}

So you need to first access "widget", and then "order" within the widget

order = json.getJSONObject("widget").getJSONObject("order")

Upvotes: 3

PSR
PSR

Reputation: 40318

what ever the json data returning by getTheAboveStringAsJSON() method there is no data with

order key

json.getJSONObject("widget").getJsonObject("order");

Upvotes: 0

Related Questions