我试图循环一个嵌套的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"
}
}
}
}
}
}
}发布于 2018-09-25 05:44:42
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));注意:更改搜索键以搜索任意键。这将返回值数组。
发布于 2018-09-25 04:24:33
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);
https://stackoverflow.com/questions/52490296
复制相似问题