【问题标题】:org.json.JSONException: Duplicate key "x" in JAVAorg.json.JSONException:JAVA 中的重复键“x”
【发布时间】:2013-12-03 11:26:06
【问题描述】:

我有以下 XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<bpmn2:definitions>
  <bpmn2:process id="defaultPackage.NewProcess2">
    <bpmn2:startEvent id="StartEvent_1" name="StartProcess">
      <bpmn2:outgoing>SequenceFlow_1</bpmn2:outgoing>
    </bpmn2:startEvent>
    <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:endEvent id="EndEvent_1" name="end event">
      <bpmn2:incoming>SequenceFlow_4</bpmn2:incoming>
    </bpmn2:endEvent>
  </bpmn2:process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_1">
    <bpmndi:BPMNPlane id="BPMNPlane_Process_1" bpmnElement="defaultPackage.NewProcess2">
      <bpmndi:BPMNShape id="BPMNShape_StartEvent_1" bpmnElement="StartEvent_1">
        <dc:Bounds height="36.0" width="36.0" x="60.0" y="210.0"/>
      </bpmndi:BPMNShape>
      <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>
      <bpmndi:BPMNShape id="BPMNShape_EndEvent_1" bpmnElement="EndEvent_1">
        <dc:Bounds height="36.0" width="36.0" x="842.0" y="202.0"/>
</bpmn2:definitions>

对于在&lt;bpmn2:process&gt; &lt;/bpmn2:process&gt; 标签下定义的每个 userTask 节点,我想获取它们的位置,该位置由 x、y 坐标定义,这些坐标位于:

<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>

&lt;bpmndi:BPMNDiagram&gt;&lt;/bpmndi:BPMNDiagram&gt; 标签内。

我想将 x、y 坐标附加到我构建的 JSON 字符串中,以生成每个 userTask 节点的属性。

我尝试了以下代码:

public static void generateChildNodesDefinitions(Node node) throws JSONException
    {
        if (node != null && node.hasChildNodes())
        {

            jw.key("nodes").array();

            NodeList childnodelist = node.getChildNodes();

            for (int k = 0; k < childnodelist.getLength(); k++)
            {
                Node childn = childnodelist.item(k);

                if (childn.hasAttributes() && childn.getNodeName() != "bpmn2:sequenceFlow")
                {

                    ArrayList<String> list = (ArrayList<String>) jsonValues.get(childn.getNodeName());

                    NamedNodeMap nnmchildnodes = childn.getAttributes();
                    //per cdo nyje duhet te krijoj nje json object
                    jw.object();


                    if (nnmchildnodes != null)
                    {
                        for (int j = 0; j < nnmchildnodes.getLength(); j++)
                        {


                            Node nodeUid = nnmchildnodes.item(j);

                            if (nodeUid.getNodeName().equals("id"))
                            {
                                String uid = new UID().toString();

                                hmIDs.put(nodeUid.getNodeValue(), uid);

                                nodeUid.setNodeValue(uid);

                            }



                            jw.key(nodeUid.getNodeName()).value(nodeUid.getNodeValue());
                        }
                    }


                    if (list != null && !list.isEmpty())
                    {
    List<String> myXarray = new ArrayList<String>();
                        List<String> myYarray = new ArrayList<String>(); 

                        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); 
                                      myXarray.add(x);
                                      myYarray.add(y);

                                     }
                                  }
                          }
                        }

                                      jw.key("x").value(myXarray);
                                      jw.key("y").value(myYarray);

}

【问题讨论】:

  • 在 jw.key("x").value(x) 的 for 循环中插入;和 jw.key("y").value(y);使用 jw.key(l).value(x);和 jw.key(l).value(y);并检查输出
  • l 是 int 类型,JSONWriter 类型中的方法 key(String) 不适用于参数 (int)
  • 你在构建错误的东西。您应该做的是为每个 userTask 构建一个单独的 Map,将 x 和 y 作为值插入到 Map 中,然后将 Map 插入到单个主 List 中。其他属性可以根据需要插入到 Map 中,然后整个结构可以干净地“序列化”为 JSON。
  • 我试过这个,但我得到了同样的错误......也许我没有为每个 userTask 正确定义 Map 。你能帮帮我吗?
  • 这并不难。创建一个列表。在处理每个 userTask 时,创建一个 Map 并将其插入到 List 中。将您对该 userTask 的任何信息添加到地图中。完成后,您将获得一个可以很好地序列化为 JSON 的地图列表。

标签: java xml json


【解决方案1】:

错误是因为您为每次迭代设置密钥 jw.key("x").value(x);jw.key("y").value(y);,它就像重复一样。

最好将 x 的值设置为数组然后jw.key("x").value(yourXarray);jw.key("y").value(yourYarray);

List<String> yourXarray = new ArrayList<String>();
 for(int l=0; l<idUserTask.size(); l++){ 
    if(idUserTask.get(l).equals(bpmnElement)){
      ArrayList<String> Xs = getElementAttributes ("dc:Bounds" , "x");
       String x = Xs.get(j);
       yourXarray.add(x)
     }}
 jw.key("x").value(yourXarray);

【讨论】:

  • x 的值都已放入数组中:ArrayList Xs = getElementAttributes ("dc:Bounds" , "x"); 我知道问题出在每次迭代都会复制 x,但我不知道如何解决这个问题
  • 你期望的 json 字符串是什么?如果是这样的"id": "UserTask_1" "name":"User Task 1","x":"165.0","y":"205.0", "x":"16.0","y":"20.0" 我认为你不能这样做,因为你有重复的“x”和“y”
  • 不,我希望它是:{"id":"UserTask_1" "name":"User Task 1","x":"165.0","y":"205.0"} for第一个 userTask 节点和: {"id": "UserTask_2" "name":"User Task 2","x":"550.0","y":"203.0"} 第二个。对于每个节点,我将它们的属性放入 Json 数组中
  • 请向您的问题添加更多代码,例如创建 JSONObject JSONWriter 以及如何打印 json 字符串
  • 我按照你说的编辑了我的代码(有一点不同,请检查),现在我的输出是: {"id":"UserTask_1","name":"User Task 1"," x":["165.0","550.0"],"y":["205.0","203.0"],} {"id":"UserTask_2","name":"用户任务 2","x" :["165.0","550.0"],"y":["205.0","203.0"],}
猜你喜欢
  • 2013-07-22
  • 2021-10-24
  • 1970-01-01
  • 2018-06-18
  • 2020-07-17
  • 2013-10-13
  • 2013-07-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多