Tuesday, December 3, 2013

org.json.JSONException: Duplicate key








Hello everyone! I need some precious help ...







I have the following XML file :








<?xml version="1.0" encoding="UTF-8"?>







<bpmn2:definitions>







<bpmn2:process id="defaultPackage.NewProcess2">







...







<bpmn2:userTask id="UserTask_1" name="User Task 1">







<bpmn2:incoming>SequenceFlow_1</bpmn2:incoming>







<bpmn2:outgoing>SequenceFlow_2</bpmn2:outgoing>







</bpmn2:userTask>







<bpmn2:userTask id="UserTask_2" name="User Task 2">







<bpmn2:incoming>SequenceFlow_3</bpmn2:incoming>







<bpmn2:outgoing>SequenceFlow_4</bpmn2:outgoing>







</bpmn2:userTask>







...







</bpmn2:process>







<bpmndi:BPMNDiagram id="BPMNDiagram_1">







...







<bpmndi:BPMNShape id="BPMNShape_UserTask_1" bpmnElement="UserTask_1">







<dc:Bounds height="50.0" width="110.0" x="165.0" y="205.0"/>







</bpmndi:BPMNShape>







<bpmndi:BPMNShape id="BPMNShape_UserTask_2" bpmnElement="UserTask_2">







<dc:Bounds height="50.0" width="110.0" x="550.0" y="203.0"/>







</bpmndi:BPMNShape>







...







</bpmn2:definitions>








For each userTask node defined under <bpmn2:process> </bpmn2:process> tags, I want to get theire position which is defined by x, y coordinates, which are found under :







<bpmndi:BPMNShape id="BPMNShape_UserTask_1" bpmnElement="UserTask_1">







<dc:Bounds height="50.0" width="110.0" x="165.0" y="205.0"/>







</bpmndi:BPMNShape>







inside <bpmndi:BPMNDiagram></bpmndi:BPMNDiagram> tags.








I want to append x, y coordinates to the JSON string I built to generate the attributes of every userTask node.








I tried the following code :












Java Code:











ArrayList<String> bpmnElements = getElementAttributes("bpmndi:BPMNShape", "bpmnElement");
for (int j = 0; j < bpmnElements.size(); j++)
{
String bpmnElement = bpmnElements.get(j);
if(childn.getNodeName().equals("bpmn2:userTask")){

ArrayList<String> idUserTask = getIds("bpmn2:userTask");

for(int l=0; l < idUserTask.size(); l++){
if(idUserTask.get(l).equals(bpmnElement)){

ArrayList<String> Xs = getElementAttributes ("dc:Bounds" , "x");
ArrayList<String> Ys = getElementAttributes ("dc:Bounds" , "y");

String x = Xs.get(j);
String y = Ys.get(j);
jw.key("x").value(x);
jw.key("y").value(y);

}
}
}
}









but I get the following exception:







org.json.JSONException: Duplicate key "x"







at org.json.JSONObject.putOnce(JSONObject.java:1121)







at org.json.JSONWriter.key(JSONWriter.java:207)







at Main.generateChildNodesDefinitions(Main.java:422)







at Main.main(Main.java:165)



This happens when I have more than one userTask node in my XML file, becouse when I have only one userTask node it gives me the correct output:







{"id":"UserTask_1","name":"User Task 1","x":"165.0","y":"205.0"}








When I have two or more userTask nodes in my xml file I want my output to be:







{"id":"UserTask_1","name":"User Task 1","x":"165.0","y":"205.0"}







{"id":"UserTask_2","name":"User Task 2","x":"550.0", "y":"203.0"}








I think my problem is because you I am setting the key jw.key("x").value(x);jw.key("y").value(y); for each iteration it will be like a duplicate .







I've tried almost everything to resolve this but no solution at all...








Any help please ...?















No comments:

Post a Comment