读取特定的JSON数据

时间:2018-07-31 10:29:01

标签: javascript json

我正在尝试处理一些JSON数据并读取其中的特定部分。 这是我的输出示例:

 {
      "operators": {
        "operator1": {
          "top": 20,
          "left": 20,
          "properties": {
            "title": "Input 1",
            "inputs": {},
            "outputs": {
              "output_1": {
                "label": "Output 1"
              }
            }
          }
        },
        "operator2": {
          "top": 80,
          "left": 300,
          "properties": {
            "title": "operator2",
            "inputs": {
              "input_1": {
                "label": "Input 1"
              }
            },
            "outputs": {
              "output_1": {
                "label": "out-1"
              },
              "output_2": {
                "label": "out-2"
              }
            }
          }
        },
        "created_op_0": {
          "top": 60,
          "left": 500,
          "properties": {
            "title": "title",
            "inputs": {
              "input_1": {
                "label": "Input 1"
              }
            },
            "outputs": {}
          }
        }
      },
      "links": {
        "0": {
          "fromOperator": "operator1",
          "fromConnector": "output_1",
          "fromSubConnector": 0,
          "toOperator": "operator2",
          "toConnector": "input_1",
          "toSubConnector": 0
        },
        "1": {
          "fromOperator": "operator2",
          "fromConnector": "output_1",
          "fromSubConnector": 0,
          "toOperator": "created_op_0",
          "toConnector": "input_1",
          "toSubConnector": 0
        }
      },
      "operatorTypes": {}
    }

我只想阅读links
我没有找到有关如何执行此操作的任何示例。谁能指出我正确的方向。

我如何使用javascript / jquery读取并循环浏览?

谢谢

1 个答案:

答案 0 :(得分:2)

您可以循环到links属性的键来获取links对象中的所有对象:

var data = {
      "operators": {
        "operator1": {
          "top": 20,
          "left": 20,
          "properties": {
            "title": "Input 1",
            "inputs": {},
            "outputs": {
              "output_1": {
                "label": "Output 1"
              }
            }
          }
        },
        "operator2": {
          "top": 80,
          "left": 300,
          "properties": {
            "title": "operator2",
            "inputs": {
              "input_1": {
                "label": "Input 1"
              }
            },
            "outputs": {
              "output_1": {
                "label": "out-1"
              },
              "output_2": {
                "label": "out-2"
              }
            }
          }
        },
        "created_op_0": {
          "top": 60,
          "left": 500,
          "properties": {
            "title": "title",
            "inputs": {
              "input_1": {
                "label": "Input 1"
              }
            },
            "outputs": {}
          }
        }
      },
      "links": {
        "0": {
          "fromOperator": "operator1",
          "fromConnector": "output_1",
          "fromSubConnector": 0,
          "toOperator": "operator2",
          "toConnector": "input_1",
          "toSubConnector": 0
        },
        "1": {
          "fromOperator": "operator2",
          "fromConnector": "output_1",
          "fromSubConnector": 0,
          "toOperator": "created_op_0",
          "toConnector": "input_1",
          "toSubConnector": 0
        }
      },
      "operatorTypes": {}
    };
Object.keys(data.links).forEach(function(key){
  console.log(data.links[key]);
});