用于转换JSON结构的Javascript递归函数

时间:2018-05-21 10:19:07

标签: javascript json logic

我有一个JSON对象,我需要使用Javascript函数将其转换为另一个JSON结构。 我的输入JSON是:

{
    "id": 116,
    "name": "flow",
    "op_start": 9,
    "op_start_type": "web_hook",
    "style": {
        "x": 41,
        "y": 35,
        "width": 250,
        "height": 250
    },
    "modules": {
        "web_hook": {
            "8": {
                "id": 8,
                "request_body": "auth",
                "name": "nameew",
                "op_success": null,
                "op_failure": null,
                "op_success_type": null,
                "op_failure_type": null,
                "style": {
                    "x": 628,
                    "y": 70,
                    "width": 250,
                    "height": 250
                }
            },
            "9": {
                "id": 9,
                "request_body": "auth",
                "name": "testontest",
                "op_success": 1,
                "op_failure": null,
                "op_success_type": "ring",
                "op_failure_type": null,
                "style": {
                    "x": 438,
                    "y": 140,
                    "width": 250,
                    "height": 250
                }
            }
        },
        "ring": {
            "1": {
                "id": 1,
                "request_body": "auth",
                "name": "testontest",
                "op_success": null,
                "op_failure": null,
                "op_success_type": null,
                "op_failure_type": null,
                "style": {
                    "x": 438,
                    "y": 140,
                    "width": 250,
                    "height": 250
                }
            }
        }
    }
}

,所需的输出是:

{
    "nodes": [
        {
            "name": "flow",
            "id": 116,
            "x": 41,
            "y": 35,
            "width": 250,
            "inputConnectors": [],
            "outputConnectors": [
                {
                    "name": "op_start"
                }
            ]
        },
        {
            "name": "web_hook",
            "id": 8,
            "x": 628,
            "y": 70,
            "inputConnectors": [
                {
                    "name": "iA"
                }
            ],
            "outputConnectors": [
                {
                    "name": "op_success"
                },
                {
                    "name": "op_failure"
                }
            ],
            "width": 250
        },
        {
            "name": "ring",
            "id": 9,
            "x": 438,
            "y": 140,
            "inputConnectors": [
                {
                    "name": "iA"
                }
            ],
            "outputConnectors": [
                {
                    "name": "op_success"
                }
            ],
            "width": 250
        }
    ],
    "connections": [
        {
            "source": {
                "nodeID": 116,
                "connectorIndex": 0
            },
            "dest": {
                "nodeID": 8,
                "connectorIndex": 0
            }
        },
        {
            "source": {
                "nodeID": 8,
                "connectorIndex": 0
            },
            "dest": {
                "nodeID": 9,
                "connectorIndex": 0
            }
        }
    ]
}

这里我们可以在输出JSON中看到第一个节点flow将被修复。 我试过这样的事情:

var resData = response.data; // input json is inside response.data
 // Creating first fixed node
  var newDefinition = {
      "nodes": [
          {
              "name": "flow",
              "id": resData.id,
              "x": resData.x,
              "y": resData.y,
              "width": resData.width,
              "inputConnectors": [],
              "outputConnectors": [
                  {
                      "name": "op_start"
                  }
              ]
          }
      ],
     "connections": []
  };

  // Getting the first connected node
  var nextType = resData.op_start_type;
  var nextId = resData.op_start;
  var next = resData.modules[nextType][nextId];

  newDefinition.nodes.push({
      "name": next.name,
      "id": next.id,
      "x": next.style.x,
      "y": next.style.y,
      "width": next.style.width,
      "inputConnectors": [
        {
          name: ''
        }
    ],
      "outputConnectors": [
          {
              "name": "op_start"
          }
      ]
  });

直到这里一切都很好,但现在我很困惑如何递归添加节点,我还需要添加连接。

感谢。

修改

这是输出JSON生成的输出 enter image description here

0 个答案:

没有答案