Angular4中的嵌套JSON循环

时间:2018-09-25 03:47:46

标签: json angular typescript

我正在尝试遍历嵌套的json,但是我无法实现输出,任何人都可以帮助我从下面显示的json中带出值,在这里我想获取bpmn:startEvent id值。

{  
   "bpmn:definitions":{  
      "@attributes":{  
         "xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance",
         "xmlns:bpmn":"http://www.omg.org/spec/BPMN/20100524/MODEL",
         "xmlns:bpmndi":"http://www.omg.org/spec/BPMN/20100524/DI",
         "xmlns:dc":"http://www.omg.org/spec/DD/20100524/DC",
         "id":"Definitions_1",
         "targetNamespace":"http://bpmn.io/schema/bpmn"
      },
      "bpmn:process":{  
         "@attributes":{  
            "id":"Process_1",
            "isExecutable":"false"
         },
         "bpmn:startEvent":{  
            "@attributes":{  
               "id":"StartEvent_1"
            }
         }
      },
      "bpmndi:BPMNDiagram":{  
         "@attributes":{  
            "id":"BPMNDiagram_1"
         },
         "bpmndi:BPMNPlane":{  
            "@attributes":{  
               "id":"BPMNPlane_1",
               "bpmnElement":"Process_1"
            },
            "bpmndi:BPMNShape":{  
               "@attributes":{  
                  "id":"_BPMNShape_StartEvent_2",
                  "bpmnElement":"StartEvent_1"
               },
               "dc:Bounds":{  
                  "@attributes":{  
                     "x":"173",
                     "y":"102",
                     "width":"36",
                     "height":"36"
                  }
               }
            }
         }
      }
   }
}

2 个答案:

答案 0 :(得分:1)

var myJson = {
  "bpmn:definitions": {
    "@attributes": {
      "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
      "xmlns:bpmn": "http://www.omg.org/spec/BPMN/20100524/MODEL",
      "xmlns:bpmndi": "http://www.omg.org/spec/BPMN/20100524/DI",
      "xmlns:dc": "http://www.omg.org/spec/DD/20100524/DC",
      "id": "Definitions_1",
      "targetNamespace": "http://bpmn.io/schema/bpmn"
    },
    "bpmn:process": {
      "@attributes": {
        "id": "Process_1",
        "isExecutable": "false"
      },
      "bpmn:startEvent": {
        "@attributes": {
          "id": "StartEvent_1"
        }
      }
    },
    "bpmndi:BPMNDiagram": {
      "@attributes": {
        "id": "BPMNDiagram_1"
      },
      "bpmndi:BPMNPlane": {
        "@attributes": {
          "id": "BPMNPlane_1",
          "bpmnElement": "Process_1"
        },
        "bpmndi:BPMNShape": {
          "@attributes": {
            "id": "_BPMNShape_StartEvent_2",
            "bpmnElement": "StartEvent_1"
          },
          "dc:Bounds": {
            "@attributes": {
              "x": "173",
              "y": "102",
              "width": "36",
              "height": "36"
            }
          }
        }
      }
    }
  }
};

console.log(myJson["bpmn:definitions"]["bpmn:process"]["bpmn:startEvent"]["@attributes"].id);

答案 1 :(得分:1)

var getAllValuesOfKey = function (dataObj, queryKey) {
    var resultArr = [];
    if (!queryKey) {
        return resultArr;
    }

    function execute(dataObj, queryKey) {
        Object.keys(dataObj).forEach(function (key, index) {
            if (typeof dataObj[key] == 'object' && !(dataObj[key] instanceof Array)) {
                if (key == queryKey) {
                    resultArr.push(dataObj[key]);
                }
                execute(dataObj[key], queryKey);
            } else if (key == queryKey) {
                resultArr.push(dataObj[key]);
            }
        });
    }
    execute(dataObj, queryKey);
    return resultArr;
}

var searchKey = 'bpmn:startEvent';

console.log(getAllValuesOfKey(myJson, searchKey));

注意:更改搜索键以搜索任何键。这将返回值数组。