Lexahen
Lexahen

Reputation: 15

JSONObject["name"] not found

When I try to get a JSONObject from a JSONArray I get the following error:

JSONObject["name"] not found.

My code is:

System.out.println(jsonArray);
for (int k = 0; k < jsonArray.length(); k++) {
    System.out.println(jsonArray.get(k));
    obt = new JSONObject(jsonArray.get(k));
    System.out.println(obt);
    objectName = obt.getString("name");
}

The log is:

10:47:15,107 INFO [stdout] (taskScheduler-1) [{"name":"(Id)\|(decimal)","value":"0"}]

10:47:15,107 INFO [stdout] (taskScheduler-1) {"name":"(Id)\|(decimal)","value":"0"}

10:47:15,108 INFO [stdout] (taskScheduler-1) {}

So I understand that it is not obt.getString("name") the problem but obt = new JSONObject(jsonArray.get(k));. jsonArray and obt are correctly defined.

Upvotes: 0

Views: 1211

Answers (1)

Guilherme Mussi
Guilherme Mussi

Reputation: 1057

You can just to it like this inside your loop:

objectName = jsonArray.get(k).getString("name");

Upvotes: 1

Related Questions