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

时间:2018-07-10 14:20:08

标签: javascript 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"]
              }]
          }]
      }]
  }
}

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

3 个答案:

答案 0 :(得分:2)

您可以使用数组符号访问“对象”属性,例如:

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

如果键之间有特殊字符或空格,则需要使用['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])