Nabil Malloul Nabil Malloul - 1 month ago 12
JSON Question

Create JSONObject from string

I know that this question was already asked, i tried all solutions but nothing works for me.
So i have this json syntax string:

{
tasks: [
{
blockId: "startpoint1",
properties: [ "aaaa" ]
},
{
blockId: "endpoint2",
properties: [ "tttttt" ]
}
]
}


I tried to create a
JSONObject
from this String by this way:

JSONParser parser=new JSONParser();
try {
JSONObject json=(JSONObject) parser.parse(req.getParameter(WORKFLOW_DEFINITION_PROPERTIES));
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}


Now i want to loop on the
tasks
array to get every element
blockId
. I tried to do this by casting the
JSONObject
to
JSONArray
like this:

JSONArray tasks=(JSONArray) json.get("tasks");


but i still enable to loop over
tasks
to get the
blockId's
.
Can you tell me what i made wrong or how to fix this?

Answer Source

Just change the way you create your JSONObject.

JSONObject jObject = new JSONObject(jsonStr);
//later you can access to your array 
JSONArray tasks=(JSONArray) jObject.get("tasks");