Results 1 to 12 of 12
  1. #1
    Join Date
    Jan 2013
    Posts
    64
    Rep Power
    0

    Default 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 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 ...?
    Last edited by blertadhimitri; 12-04-2013 at 10:07 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default Re: org.json.JSONException: Duplicate key

    You need at to be adding this as an array of elements (user tasks?).
    At the moment you seem to have a single element containing and id, a name, and x/y, to which you then try and add another x (or y), hence the error.

    You need to go through the documentation for the JSON library you're using and see how to go about doing this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Join Date
    Jan 2013
    Posts
    64
    Rep Power
    0

    Default Re: org.json.JSONException: Duplicate key

    I just want that for each userTask node, to take it's x,y coordinates and include them in the json string that I generate for all the attributes of every userTask node.
    My problem is that I don't know how to tell my program to take the x, y coordinates from Xs and Ys arrays that correspond to each userTask node....

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default Re: org.json.JSONException: Duplicate key

    I don't know how else I can describe it.
    You have (at the moment) a single element in your JSONWriter.
    The code above doesn't show where you are writing the id and name attributes of this object.
    I would say that's your problem.

    Well, to be honest, I might consider turning this XML into JAXB objects and work with those. It would save you having to keep going back to the xml to find certain elements as Strings.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Join Date
    Jan 2013
    Posts
    64
    Rep Power
    0

    Default Re: org.json.JSONException: Duplicate key

    Actually I have the code where I write the id and name attributes and it works ok ...
    My problem is this part of the code ... where i have to get x and y, which are not part
    of the bpmn2:userTask attributes, becouse they are given separately as i described
    before

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default Re: org.json.JSONException: Duplicate key

    OK, you're going to need to provide a complete (short) set of code that shows exactly how this is supposed to work, because the code above will not work for more than 1 item for the reasons I state above.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Join Date
    Jan 2013
    Posts
    64
    Rep Power
    0

    Default Re: org.json.JSONException: Duplicate key

    Java Code:
    NamedNodeMap nnmchildnodes = childn.getAttributes();
    					
    					jw.object();
    					
    			
    					if (nnmchildnodes != null)
    					{
    						for (int j = 0; j < nnmchildnodes.getLength(); j++)
    						{
    							Node nodeattributes = nnmchildnodes.item(j);
    							
    							jw.key(nodeattributes.getNodeName()).value(nodeattributes.getNodeValue());
    						}
    					}
    
                                                  if (list != null && !list.isEmpty())
    					   
                                                        {  
                                                          ArrayList<String> bpmnElements = getElementAttributes("bpmndi:BPMNShape", "bpmnElement");
      ....
      ....
      ....

  8. #8
    Join Date
    Jan 2013
    Posts
    64
    Rep Power
    0

    Default Re: org.json.JSONException: Duplicate key

    As I have said before my problem is not in the way I generate the JSON string for the other attributes of my nodes...
    All I want to solve is something wrong I do in the first slice of code of my first post in this thread.
    When I have only one bpmn2:userTask node it shows me the correct output becouse the arrays Xs and Ys of the coordinates of
    the bpmn2:userTask nodes have only one element, so one x and one y respectively.
    Then when I have two bpmn2:userTask nodes in my xml file, the arrays Xs and Ys have two x coordinates and two y coordinates.
    And my error consists that i create two jw.key("x") .value(x) for each bpmn2:userTask node. Thats my error.
    I only need to put the correct jw.key("x") .value(x) for the bpmn2:userTask node with id "UserTask_1" and
    the correct jw.key("x") .value(x) for the bpmn2:userTask node with id "UserTask_2"
    Last edited by blertadhimitri; 12-05-2013 at 10:49 AM.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default Re: org.json.JSONException: Duplicate key

    Java Code:
    NamedNodeMap nnmchildnodes = childn.getAttributes();
    So at this point, this node map is one of these:
    Java Code:
    <bpmn2:userTask id="UserTask_1" name="User Task 1">
    is that correct?

    You then get the bpmnElements from all of these:
    Java Code:
    <bpmndi:BPMNShape id="BPMNShape_UserTask_1" bpmnElement="UserTask_1">
    loop over them.
    For each one you then get the ids from the userTask nodes, and loop over them.
    Why?
    Surely at this point you know which id you're looking for from step 1?
    What you want to be doing is looking for the node that has that id.

    That whole loop around the idUserTask list is pointless, and is the root of your problem.
    You don't want to be going over that entire list, you just want to find the one element you are interested in.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    Join Date
    Jan 2013
    Posts
    64
    Rep Power
    0

    Default Re: org.json.JSONException: Duplicate key

    I take userTask ids becouse they are equal to the bpmnElement attribute of the <bpmndi:BPMNShape>
    which contains the <dc:Bounds> childnode where the coordinates x,y are defined for each type of node.
    In this way I make the correspondence of each node with the correct couple of x,y coordinates.
    I have controlled this part and this is done correctly.
    I just don't know how to tell my program to put x, y values that coorrespond to each userTask node
    to each the json object of these nodes
    Last edited by blertadhimitri; 12-05-2013 at 12:28 PM.

  11. #11
    Join Date
    Jan 2013
    Posts
    64
    Rep Power
    0

    Default Re: org.json.JSONException: Duplicate key

    This is how it looks when I make a debug :

    org.json.JSONException: Duplicate key-1.png
    org.json.JSONException: Duplicate key-2.png

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default Re: org.json.JSONException: Duplicate key

    Java Code:
    ArrayList<String> idUserTask = getIds("bpmn2:userTask");
    that gets every userTask id.
    That code is in a loop around this list:
    Java Code:
    ArrayList<String> bpmnElements = getElementAttributes("bpmndi:BPMNShape", "bpmnElement");
    which is every bpmnElement (ie userTask id) in the other xml.

    You don't want that first list.
    You want to simply use the id of the object you are currently building.

    So, which ever one of these nodes:
    Java Code:
    NamedNodeMap nnmchildnodes = childn.getAttributes();
    happens to be the id.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Ask Json
    By ultra.coder in forum Advanced Java
    Replies: 0
    Last Post: 11-19-2013, 04:08 AM
  2. Replies: 5
    Last Post: 06-03-2012, 07:20 PM
  3. Web API using JSON/POST
    By atomant in forum New To Java
    Replies: 3
    Last Post: 02-17-2011, 10:56 PM
  4. Json
    By mac in forum New To Java
    Replies: 10
    Last Post: 06-08-2010, 11:15 PM
  5. JSON Lib: json-lib-1.0-jdk13.jar
    By Raghuraman K in forum Advanced Java
    Replies: 3
    Last Post: 05-13-2010, 10:00 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •