访问键值中带有':'的 JSON 值



我正在用javascript开发,我需要访问JSON中的值,如下所示:

{
"soap:Envelope": {
"$": {
"xmlns:soap": "http://schemas.xmlsoap.org/soap/envelope/",
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
"xmlns:wsa": "http://schemas.xmlsoap.org/ws/2004/08/addressing",
"xmlns:wsse": "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
"xmlns:wsu": "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
},
"soap:Header": [{
"wsa:Action": ["RetrieveResponse"],
"wsa:MessageID": ["urn:uuid:1a13717a-dc14-4379-b0d0-51065965d49a"],
"wsa:RelatesTo": ["urn:uuid:242f637e-9ce4-485e-ba8c-e2149494bb57"],
"wsa:To": ["http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous"],
"wsse:Security": [{
"wsu:Timestamp": [{
"$": {
"wsu:Id": "Timestamp-6ca32f3e-f16e-4ce1-af48-ba04df471780"
},
"wsu:Created": ["2018-07-10T13:59:07Z"],
"wsu:Expires": ["2018-07-10T14:04:07Z"]
}]
}]
}],
"soap:Body": [{
"RetrieveResponseMsg": [{
"$": {
"xmlns": "http://exacttarget.com/wsdl/partnerAPI"
},
"OverallStatus": ["OK"],
"RequestID": ["33593cff-ff63-4c39-8d80-43312f6c4715"],
"Results": [{
"$": {
"xsi:type": "QueryDefinition"
},
"PartnerKey": [{
"$": {
"xsi:nil": "true"
}
}],
"ObjectID": ["37e75ee6-9a5b-4a35-b3f8-be036a5889c1"],
"Name": ["Scoring-Query"]
}]
}]
}]
}
}

我需要像result.soap:Envelope.soap:Body.一样访问"ObjectID": ["37e75ee6-9a5b-4a35-b3f8-be036a5889c1"],,但由于字符":",我无法获得它。我试图逃避它,但它没有奏效。感谢您的帮助!

您可以使用数组表示法访问 Object 属性,例如:

result['soap:Envelope']['soap:Body']

如果键之间有特殊字符或空格,则需要使用 ['key']

var result = {
"soap:Envelope": {
"$": {
"xmlns:soap": "http://schemas.xmlsoap.org/soap/envelope/",
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
"xmlns:wsa": "http://schemas.xmlsoap.org/ws/2004/08/addressing",
"xmlns:wsse": "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
"xmlns:wsu": "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
},
"soap:Header": [{
"wsa:Action": ["RetrieveResponse"],
"wsa:MessageID": ["urn:uuid:1a13717a-dc14-4379-b0d0-51065965d49a"],
"wsa:RelatesTo": ["urn:uuid:242f637e-9ce4-485e-ba8c-e2149494bb57"],
"wsa:To": ["http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous"],
"wsse:Security": [{
"wsu:Timestamp": [{
"$": {
"wsu:Id": "Timestamp-6ca32f3e-f16e-4ce1-af48-ba04df471780"
},
"wsu:Created": ["2018-07-10T13:59:07Z"],
"wsu:Expires": ["2018-07-10T14:04:07Z"]
}]
}]
}],
"soap:Body": [{
"RetrieveResponseMsg": [{
"$": {
"xmlns": "http://exacttarget.com/wsdl/partnerAPI"
},
"OverallStatus": ["OK"],
"RequestID": ["33593cff-ff63-4c39-8d80-43312f6c4715"],
"Results": [{
"$": {
"xsi:type": "QueryDefinition"
},
"PartnerKey": [{
"$": {
"xsi:nil": "true"
}
}],
"ObjectID": ["37e75ee6-9a5b-4a35-b3f8-be036a5889c1"],
"Name": ["Scoring-Query"]
}]
}]
}]
}
};
console.log(result['soap:Envelope']['soap:Body'][0].RetrieveResponseMsg[0].Results[0].ObjectID[0])

将其括在数组中并用引号括起来['example:example']

最新更新