首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular4中的嵌套JSON循环

Angular4中的嵌套JSON循环
EN

Stack Overflow用户
提问于 2018-09-25 03:47:45
回答 2查看 82关注 0票数 0

我试图循环一个嵌套的json,但我无法获得输出,请任何人帮助我从json获得如下所示的值,这里我想得到bpmn:startEvent id值。

代码语言:javascript
复制
{  
   "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"
                  }
               }
            }
         }
      }
   }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-25 05:44:42

代码语言:javascript
复制
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));

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

票数 1
EN

Stack Overflow用户

发布于 2018-09-25 04:24:33

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52490296

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档