Reputation: 83
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?
Upvotes: 1
Views: 6647
Reputation: 978
Do you need this?
var arrayVariable=[
{
blockId: "startpoint1",
properties: [ "aaaa" ]
},
{
blockId: "endpoint2",
properties: [ "tttttt" ]
}
]
arrayVariable.map(function(d){
return d.blockId
});
Out put you will get all you blockId as
["startpoint1", "endpoint2"]
Upvotes: -1
Reputation: 5948
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");
Upvotes: 1
Reputation: 2048
You have to use getJsonArray
method instead of get
method to retrieve the array of tasks :
JSONArray tasks= json.getJsonArray("tasks");
Upvotes: 4