通过javascript或jQuery仅从JSON获取特定对象

时间:2017-11-16 12:09:25

标签: javascript jquery json

我想从以下JSON中仅获取特定对象,例如只有那些具有classDefinition =" com.sap.bpm.wfs.UserTask"的JSON对象。请建议如何执行此操作:

var metadata = {
    "contents": {
        "83eaead8-cfae-459b-9bdd-8b12e32d6715": {
            "classDefinition": "com.sap.bpm.wfs.StartEvent",
            "id": "startevent1",
            "name": "StartEvent1"
        },
        "13583ac9-596d-4375-b9e1-e5f6f21e829f": {
            "classDefinition": "com.sap.bpm.wfs.EndEvent",
            "id": "endevent1",
            "name": "EndEvent1"
        },
        "6c2b0935-444b-4299-ac8e-92973ce93558": {
            "classDefinition": "com.sap.bpm.wfs.UserTask",
            "subject": "Upload document",
            "description": "{context.description}",
            "priority": "MEDIUM",
            "isHiddenInLogForParticipant": false,
            "userInterface": "sapui5://html5apps/saptest/com.sap.test",
            "recipientUsers": "I311520, I310811",
            "id": "usertask1",
            "name": "UserTask1"
        },
        "6728bf81-3d4e-4ae3-a428-1700a2096d34": {
            "classDefinition": "com.sap.bpm.wfs.SequenceFlow",
            "id": "sequenceflow1",
            "name": "SequenceFlow1",
            "sourceRef": "83eaead8-cfae-459b-9bdd-8b12e32d6715",
            "targetRef": "6c2b0935-444b-4299-ac8e-92973ce93558"
        },
        "aa99931e-2523-44c3-86b3-d522acdbde10": {
            "classDefinition": "com.sap.bpm.wfs.ui.Diagram",
            "symbols": {
                "760f0725-3400-4d48-b082-5c69ad79d697": {},
                "aa9a0d10-63be-4af8-9ac2-4d2b648a18fc": {},
                "7fbd11bb-cf82-4a27-97d7-e80dda2014ee": {},
                "20c66c48-6058-465e-b500-d69d6e54c028": {},
                "2e8f324c-5361-4512-a09a-fc7693f206ba": {}
            }
        }
    }
};

3 个答案:

答案 0 :(得分:2)

首先,metadata.contents属性应该是一个数组。 如果您确实无法将其更改为数组,请使用Object.keys(metadata.contents)

例如:

Object.keys(metadata.contents)
  .map(x => metadata.contents[x])
  .filter(x => x.classDefinition == 'com.sap.bpm.wfs.UserTask')

var metadata = {
    "contents": {
        "83eaead8-cfae-459b-9bdd-8b12e32d6715": {
            "classDefinition": "com.sap.bpm.wfs.StartEvent",
        },
        "13583ac9-596d-4375-b9e1-e5f6f21e829f": {
            "classDefinition": "com.sap.bpm.wfs.EndEvent",
        },
        "6c2b0935-444b-4299-ac8e-92973ce93558": {
            "classDefinition": "com.sap.bpm.wfs.UserTask",
            "subject": "Upload document",
            "description": "{context.description}",
            "priority": "MEDIUM",
            "isHiddenInLogForParticipant": false,
            "userInterface": "sapui5://html5apps/saptest/com.sap.test",
            "recipientUsers": "I311520, I310811",
            "id": "usertask1",
            "name": "UserTask1"
        },
        "6728bf81-3d4e-4ae3-a428-1700a2096d34": {
            "classDefinition": "com.sap.bpm.wfs.SequenceFlow",
        },
        "aa99931e-2523-44c3-86b3-d522acdbde10": {
            "classDefinition": "com.sap.bpm.wfs.ui.Diagram",
        }
    }
}

var filtered = Object.keys(metadata.contents)
  .map(x => metadata.contents[x])
  .filter(x => x.classDefinition == 'com.sap.bpm.wfs.UserTask')
  
console.log(filtered)

答案 1 :(得分:1)

可以使用简单的for循环来获取所需的字段:

var temp = [];

for (var index in metadata.contents) {
    if (metadata.contents[index].classDefinition == "com.sap.bpm.wfs.UserTask") {
        temp.push(metadata.contents[index]);
    }
}

答案 2 :(得分:0)

或者你可以一个接一个地做



var metadata = {
    "contents": {
        "83eaead8-cfae-459b-9bdd-8b12e32d6715": {
            "classDefinition": "com.sap.bpm.wfs.StartEvent",
            "id": "startevent1",
            "name": "StartEvent1"
        },
        "13583ac9-596d-4375-b9e1-e5f6f21e829f": {
            "classDefinition": "com.sap.bpm.wfs.EndEvent",
            "id": "endevent1",
            "name": "EndEvent1"
        },
        "6c2b0935-444b-4299-ac8e-92973ce93558": {
            "classDefinition": "com.sap.bpm.wfs.UserTask",
            "subject": "Upload document",
            "description": "{context.description}",
            "priority": "MEDIUM",
            "isHiddenInLogForParticipant": false,
            "userInterface": "sapui5://html5apps/saptest/com.sap.test",
            "recipientUsers": "I311520, I310811",
            "id": "usertask1",
            "name": "UserTask1"
        },
        "6728bf81-3d4e-4ae3-a428-1700a2096d34": {
            "classDefinition": "com.sap.bpm.wfs.SequenceFlow",
            "id": "sequenceflow1",
            "name": "SequenceFlow1",
            "sourceRef": "83eaead8-cfae-459b-9bdd-8b12e32d6715",
            "targetRef": "6c2b0935-444b-4299-ac8e-92973ce93558"
        },
        "aa99931e-2523-44c3-86b3-d522acdbde10": {
            "classDefinition": "com.sap.bpm.wfs.ui.Diagram",
            "symbols": {
                "760f0725-3400-4d48-b082-5c69ad79d697": {},
                "aa9a0d10-63be-4af8-9ac2-4d2b648a18fc": {},
                "7fbd11bb-cf82-4a27-97d7-e80dda2014ee": {},
                "20c66c48-6058-465e-b500-d69d6e54c028": {},
                "2e8f324c-5361-4512-a09a-fc7693f206ba": {}
            }
        }
    }
}
var content = metadata["contents"];
var subContent = content["6c2b0935-444b-4299-ac8e-92973ce93558"];
var classDef = subContent["classDefinition"];
alert(classDef);